File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes variable declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "variable declaration" Watch "variable declaration" New topic

variable declaration

Manoj Pandey
Ranch Hand

Joined: May 03, 2001
Posts: 36
what would the difference between these scenario's wrt performance.
notice the declarations of the variables tempid and tempname !
String stArr[][]=new String[j-1][2];
for (int i=1;i<j;i++)
int k=i-1;
String tempid=((Vector)(resVec.elementAt(i))).elementAt(0)+"";
String tempname=((Vector)(resVec.elementAt(i))).elementAt(1)+"";

String stArr[][]=new String[j-1][2];
String tempid=null;
String tempname=null;
for (int i=1;i<j;i++)
int k=i-1;

Sun Certified Java Programmer
Sune Mangs

Joined: Jan 17, 2002
Posts: 3
I think the second example would be slightly slower cause you initialize the variables to null. The performance inside the loop is identical.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
I believe that unnecessary initialisation of local variables does indeed slow performance.
Additionally, it reduces the compiler's chance of detecting certain programming errors. For instance, if you declare a variable and initialise it to some arbitrary value (e.g. null, for an object reference), then use it in an expression before you set it to a sensible value, the compiler will not complain. But if you do not initialise it, then use it before setting it, the compiler should notice this and refuse to compile the code.
Two good reasons not to initialise unnecessarily.
As an aside, instance variables and static variables get initialised to well-defined values, if you do not initialise them explicitly. Object references are initialised to null, for instance. Therefore, there is no need to explicitly initialise to these values, and doing so has a small bad effect on performance. I like to do the following:

This way, you are reminded what value will be used for initialisation, but at no performance cost.
[ January 17, 2002: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Manoj Pandey
Ranch Hand

Joined: May 03, 2001
Posts: 36
Thanks for the reply Sune and Peter. What I understand is that both of you are suggesting that the second way is better, but what if the loop runs say 1000 times, would the declaration of the variables a 1000 times inside the loop not be expensive?? Isnt is less expensive to declare the variables once outside the loop once,set them to null, and and use them .
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112

Method void test()
0 aconst_null
1 astore_1
2 iconst_0
3 istore_2
4 goto 15
7 iload_2
8 invokestatic #2 <Method java.lang.String valueOf(int)>
11 astore_1
12 iinc 2 1
15 iload_2
16 bipush 10
18 if_icmplt 7
21 return

Method void test()
0 iconst_0
1 istore_1
2 goto 13
5 iload_1
6 invokestatic #2 <Method java.lang.String valueOf(int)>
9 astore_2
10 iinc 1 1
13 iload_1
14 bipush 10
16 if_icmplt 5
19 return
The only difference is the additional aconst_null & astore_1 in the first solution.
So in most applications there won't be any measurable difference between the two solutions, especially as the modern optimizing hotspot engine might even drop the aconst_null, as it isn't really needed here.
Conclusion: Choose the solution that is better readable. In fact you should always follow this advice until really *experiencing* a performance problem. See
[ January 18, 2002: Message edited by: Ilja Preuss ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: variable declaration