• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

String assignment difference

 
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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").
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic