aspose file tools*
The moose likes Beginning Java and the fly likes String assignment difference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String assignment difference" Watch "String assignment difference" New topic
Author

String assignment difference

Achint Verma
Greenhorn

Joined: Jul 13, 2012
Posts: 12
Is there any difference between the following two ways of assigning s ?


and


In both cases 's' holds the value 'foo' and 's' in both cases pass the 'instanceof' test.
So I think that the are exactly the same. Please correct me if I am wrong.

Thanks.
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

That's a good question... I'm not 100% sure of this but here's what I think:

is equivalent to:

I'll check if this is correct and come back to edit this post.

EDIT: Turns out I'm wrong. I've written this small program:

Output: s1==s2 :: false

You my guess is that String s = "foo" is simply a syntactic sugar for String s = new String("foo");. That means their are semantically the same.

EDIT: wrong (again)... turns out I was right the first time
Olivier Legat wrote:
Matthew Brown wrote:Java has some optimisations built in for handling strings - read up on the "string pool" if you're interested. s = "foo" will try to avoid creating a new object if it isn't necessary (if a "foo" String exists in the pool it's safe to reuse it because Strings are immutable). Whereas s = new String("foo") will always create a new String.


Turns out you're right (and so was I)... I ran my program again and now it outputs "s1 == s2 :: true" I don't know why it outputted false the first time.


Olly
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
String is immutable. So,
if you say


both a and b points to same foo" in the memory. But, when you declare a string with 'new', as in c, It will not refer to the same object in the memory.
It will create a new object and points to that.
you can test a and c with == operator.

here is the code:


With Regards,
Anu
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Anu satya wrote:String is immutable. So,
if you say


both a and b points to same foo" in the memory.

I'm not so sure about that... check my edit.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Java has some optimisations built in for handling strings - read up on the "string pool" if you're interested. s = "foo" will try to avoid creating a new object if it isn't necessary (if a "foo" String exists in the pool it's safe to reuse it because Strings are immutable). Whereas s = new String("foo") will always create a new String.

The bottom line is: use String s = "foo". There's almost never a good reason to use the second form (and when I say "almost never" I probably mean "never").
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Achint Verma wrote:In both cases 's' holds the value 'foo' and 's' in both cases pass the 'instanceof' test.
So I think that the are exactly the same. Please correct me if I am wrong.

It really depends on what you mean by "exactly the same". Both are equal(), but they are not '=='.

However, you almost never want to use that last type of check on Strings (see this page for more info).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Effectively those two things have the same result in the end, but the second one, where you create a new String object explicitly, is inefficient. It prevents the compiler from using Java's string pooling mechanism and creates a new String object which is totally unnecessary.

It is never necessary to write new String("some literal") - so, don't write code like that.

Matthew Brown wrote:The bottom line is: use String s = "foo". There's almost never a good reason to use the second form (and when I say "almost never" I probably mean "never").

If the string you're passing to the constructor of class String is a literal, it is certainly never necessary.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Matthew Brown wrote:Java has some optimisations built in for handling strings - read up on the "string pool" if you're interested. s = "foo" will try to avoid creating a new object if it isn't necessary (if a "foo" String exists in the pool it's safe to reuse it because Strings are immutable). Whereas s = new String("foo") will always create a new String.


Turns out you're right (and so was I)... I ran my program again and now it outputs "s1 == s2 :: true" I don't know why it outputted false the first time. Probably a mistake on my part.
 
Don't get me started about those stupid light bulbs.
 
subject: String assignment difference