Re: Bulk Array Element Allocation, is it faster?
Roedy Green wrote:
Lew wrote, quoted or indirectly quoted someone who said :
Foo#bar will be cleared to 'null' upon first construction, then initiali=
zed to 'null' by the explicit initialization.
Both steps happen. Both steps are required to happen, and they're requi=
red to happen separately.
I doubt that. The JLS never says exactly how something works, just
what the net effect must be. It is part of what makes it so difficult
Let me settle your doubt, then, because the JLS does say exactly that.
JLS =A712.5:
http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.5
"Whenever a new class instance is created, memory space is allocated for it=
with room for all the instance variables declared in the class type and al=
l the instance variables declared in each superclass of the class type, inc=
luding all the instance variables that may be hidden (=A78.3). If there is =
not sufficient space available to allocate memory for the object, then crea=
tion of the class instance completes abruptly with an OutOfMemoryError. Oth=
erwise, all the instance variables in the new object, including those decla=
red in superclasses, are initialized to their default values (=A74.12.5).
"Just before a reference to the newly created object is returned as the res=
ult, the indicated constructor is processed to initialize the new object us=
ing the following procedure:
"...
"4. Execute the instance initializers and instance variable initializers fo=
r this class, assigning the values of instance variable initializers to the=
corresponding instance variables, in the left-to-right order in which they=
appear textually in the source code for the class. If execution of any of =
these initializers results in an exception, then no further initializers ar=
e processed and this procedure completes abruptly with that same exception.=
Otherwise, continue with step 5. (In some early implementations, the compi=
ler incorrectly omitted the code to initialize a field if the field initial=
izer expression was a constant expression whose value was equal to the defa=
ult initialization value for its type.)"
to follow. A JLS for programmers rather than implementors might
describe a simple reference implementation, and say, "As a programmer
you can presume it works like this, but actual implementations will be
more sophisticated and may not give exactly the same behaviour as this
simple explanation. For those fine points, see the implementors JLS."
The JLS is extremely explicit about the order and result of steps to load, =
allocate and initialize classes and instances. In this particular, non-hyp=
othetical case, it requires members to get their default values, then separ=
ately to process explicit initializers, even (and especially) if the initia=
lization is to the same default value.
So for example it could describe how a simple GC works.
In most cases the second init to null could be dropped by a clever
optimiser because it would have the same net effect.
Except that that is, as you see from the cited passage, explicitly forbidde=
n.
Of course I defer to you and Patricia on divining the meaning of the
JLS.
You don't need to. Just read the cited passage for yourself.
--
Lew