Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Johnson Tmmet
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone say me which one of the below two options would be better in perfromance and which one would create more string objects?
One has String ,str within while loop and one outside while loop.
Thanks in advance,

Method : 1
public void printList(Vector v)
{
boolean flag = true;
int i=0;
while(flag){
String str = (String)v.get(i);
System.out.println("The value :"+ str);
if( i==5)
flag = false;
}
}


Method : 2
public void printList(Vector v)
{
boolean flag = true;
int i=0;
String str = null;
while(flag){
str = (String)v.get(i);
System.out.println("The value :"+ str);
if( i==5)
flag = false;
}
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm reading this right, the only difference is declaring the String str inside the loop or outside the loop.

We had a very similar discussion lately. The difference in performance is not enough to ever notice or worry about. The optimizer may even make them identical. I prefer declaring the variable inside the loop because it explicitly tells readers that the variable is not to be used outside the loop - no initialization required, no particular value guaranteed when the loop ends.
 
Grant Gainey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
If I'm reading this right, the only difference is declaring the String str inside the loop or outside the loop.

We had a very similar discussion lately. The difference in performance is not enough to ever notice or worry about. The optimizer may even make them identical.


Stronger statement - there is no difference. The loop code is identical in each case, no optimization involved. There is only one String reference, which is assigned values from get().

I have seen this discussion a LOT over the years, and it seems to stem from a misunderstanding of the difference between "String aStr = get();" and "String aStr = new String();". The latter creates a new Object - the former merely assigns a reference to an already-existing Object.

Originally posted by Stan James:
I prefer declaring the variable inside the loop because it explicitly tells readers that the variable is not to be used outside the loop - no initialization required, no particular value guaranteed when the loop ends.


Exactly so. Declaring in the loop is safer and (to me at least) more readable. I'd go so far as to claim it's a best-practice...

Grant
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Johnson kkkk"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic