-1

I am creating 2 string in pool of strings without making use of new keyword as shown below

String s="abc";
String s1="def";

I am creating one more by concatenating the above two strings as shown below

String s3=s+s1;
System.out.println(s3==s+s1);

the result is false.

I want to know the reason behind the result, is it because of immutability i.e. the state of a string cannot be changed.

nicopico
  • 3,571
  • 1
  • 26
  • 30
user1690808
  • 13
  • 1
  • 7

5 Answers5

3

As I think you know, the == operator compares object references, not object contents. It returns true only when the operands are the same physical object.

The reason it returns false here is simply because the concatenation is done twice, so you end up with two different computed String objects. The Java compiler is not aggressive enough to evaluate both expressions at compile time, and at runtime, the results of String concatenation are not added to the intern pool.

Ernest Friedman-Hill
  • 79,064
  • 10
  • 147
  • 183
  • it means concatenation results in a new string with same contents doesnt reference to same object.And the reason behind this is immutability,isnt it?? – user1690808 Jun 12 '13 at 13:11
  • ok..i got ur point i know that == looks for memory location and .equals look for contents here since i created two strings without using new keyword i got confused a little.Thank you for ur answer ,i just want to know that is this because of immutability the result is false – user1690808 Jun 12 '13 at 13:19
  • No, it really has nothing to do with immutability. The two computations each create an object, and as a result, you have two separate objects. If you twice called a function that returned a mutable object, then you'd similarly get two distinct objects. – Ernest Friedman-Hill Jun 12 '13 at 15:04
2

== is for reference comparison. If you need object comparison you should use .equals

Regarding String concatenation.

From http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5

Strings computed by concatenation at run time are newly created and therefore distinct.

Ajay George
  • 11,521
  • 1
  • 39
  • 46
0

Yes, Strings are immutable but a Strings created at runtime aren't singeltons. Therefore two String instances can exist that contain the same character-sequence.

Both strings are equal, but the are not identical (which is what you test with the == operator).

Robert
  • 36,354
  • 15
  • 89
  • 140
0

“==” operator compares the objects’ location(s) in memory or compares references

The Java String class actually overrides the default equals() implementation in the Object class – and it overrides the method so that it checks only the values of the strings, not their locations in memory.

so if you compare s3.equals(s+s1) then it will return true

Reference: http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/

Pritesh Shah
  • 847
  • 1
  • 7
  • 7
  • yeah i know that == looks for memory location and .equals look for contents ,since the objects are created in string of pools and not by using new keyword i was confused a little ,but the reason is that after concatenation there is new object created with same contents and it doesnt refer to s3 because of property of string i.e. immutability – user1690808 Jun 12 '13 at 13:15
0

RULE: compare Strings with .equals()

This may be too generalistic, but for now just abide by that rule

Akintayo Olusegun
  • 1,061
  • 1
  • 10
  • 21