Local database reading edition
Java Performance Tuning : Duplicate Initialization
Reconstructed from database records, not a captured original forum layout. Dates and display names are retained as recorded; timezone and real-world identity are not inferred. Historical advice, contact details and offers are not current. Original markup is shown as inert text.
Forum index · Java - Tips Of The Day
Opening post
Asha · 2001-07-17 12:18:42
I had mentioned about "Avoid Duplicate Initialization in Object creation" in the "Performance Tuning Tip" posting. Here we take an in-depth look at the mechanisms Java uses to manage object initialization.
One of minor issue with instantiating objects: When an object is
instantiated, all of its instance variables automatically are initialized to
their default values. If the instance variables are explicitly
initialized to their default values, this will duplicate the default
initialization, generate additional bytecodes, and make the instantiation
take longer.
Here are the questions that might rise:
1)when the instance variables are getting initialized?
When the object's memory is allocated, all of the fields are cleared (set to all 0 bits). The default values for all primitive fields is the value with all zero bits set.
2)why i am saying it is duplicate initialization?, Is it getting initiazlized more than one time ?
If you explicitly initialize the fields to their default values, code is added to the constructor to perform this initialization. But the values have already been set to the default values when the object was created.
For example, here is a class Test with default instace variable initialization
<pre id=code><font face=courier size=2 id=code>
public class Test {
private String name;
private long number;
private boolean result;
private float totalMarks;
public Test(){ }
...
}
</font id=code></pre id=code>
These instance variables are automatically initilized to their default values when you create an instance of class Test :
1) String(object reference) is initialized to null : name = null
2) long is initialized to 0L : number = 0L;
3) boolean is initialized to flase : result = false;
4) float is initialized to 0.0f : totalMarks = 0.0f
Asha Balasubramanyan
http://www.ammu.org
Edited by - asha on July 17 2001 12:19:32 PM
Source notes
MyCommunity/nandighosha.org.zip!/forum/tools/snitz_forums_2000.mdb
Table: FORUM_TOPICS + FORUM_REPLY · record IDs: 227