Mikhail Loenko wrote:

Hello

May be it is obvious and everybody knows it from babyhood, but anyway...

Everybody knows that this two examples of code do the same:
class test {
   public Object field = null;
}

and

class test {
   public Object field;
}

But if you disassemble these two classes, you'll see that the first example
has a 6 instruction constructor:

  0: aload_0
  1: invokespecial #1; //Method java/lang/Object."<init>":()V
  4: aload_0
  5: aconst_null
  6: putfield #2; //Field field:Ljava/lang/Object;
  9: return

while the second one has only 3 of them:
  0: aload_0
  1: invokespecial #1; //Method java/lang/Object."<init>":()V
  4: return

So having explicit assignments of default values slows down constructor.

Thanks,
Mikhail
Mikhail,

Is this something a bytecode compiler should be able to optimize away, or is there something in the JLS that requires the additional bytecodes to be generated ? Have you measured the impact (I suspect it is negligible).

cheers

Reply via email to