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.
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.
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.
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").
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?
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.
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.