aspose file tools*
The moose likes Beginning Java and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Johnson Tmmet
Greenhorn

Joined: Oct 27, 2005
Posts: 8
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Grant Gainey
Ranch Hand

Joined: Oct 16, 2005
Posts: 65
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


In Theory, there is no difference between theory and practice.<br />In Practice, there is no relationship between theory and practice.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

"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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings