Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.lightlink.com!news.iecc.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Bulk Array Element Allocation, is it faster? Date: Sun, 25 Sep 2011 11:17:30 -0700 (PDT) Organization: http://groups.google.com Lines: 67 Message-ID: <18441139.3525.1316974650414.JavaMail.geo-discussion-forums@prfh23> References: <32256761.625.1316931765221.JavaMail.geo-discussion-forums@prht31> <5mlt77d8430ejb2mnetkgoh72akphpvh2t@4ax.com> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 216.239.45.130 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1316975121 30176 127.0.0.1 (25 Sep 2011 18:25:21 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 25 Sep 2011 18:25:21 +0000 (UTC) In-Reply-To: <5mlt77d8430ejb2mnetkgoh72akphpvh2t@4ax.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=216.239.45.130; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8304 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.=20 >> Both steps happen. Both steps are required to happen, and they're requi= red to happen separately. >=20 > 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.=20 >=20 > 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. --=20 Lew