File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String assignment difference

 
Achint Verma
Greenhorn
Posts: 12
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 176
Chrome Mac Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anu satya
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9443
49
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic