-1
String a = "x";
String b = "x";
System.out.println(a==b);

It prints "true" as it shares the same memory in String constant pool. But when I write the following code,

String a = "x";
String b = a + "y";
String c = "xy";
System.out.println(b==c);

Its printing false.

I know '==' compares the instances.

My Question is - why instance is not same in the second scenario. When creating a new String, It always checks whether the same string is available in the pool or not. Then after creating String b i.e. "xy" is available in the pool. So when I'm trying to create String c with the same "xy", it should not create new instance. It should share the same memory rather than creating a new instance. Then, Why in the second scenario the instances are different??

  • dupe as well [a confusion about java String literal pool and String's concatenation](http://stackoverflow.com/questions/15427599/a-confusion-about-java-string-literal-pool-and-strings-concatenation?rq=1) –  Apr 06 '15 at 05:10
  • "When creating a new String, It always checks whether the same string is available in the pool or not." Basically, that is wrong. It's only true at compile time. – ajb Apr 06 '15 at 06:03

3 Answers3

1

here, a + "y" creates a new String object hence, == returns false. It checks for object references and not object equality.

When comparing strings, you should use the equals method.

  • == operator checks whether both references variable refer to same object
  • equals() method checks there the contents of objects are same

Learn the difference between == operator and equals method in case of strings

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
1

Problem is String is immutable, every update operation on string results into new string, and the == check the instance and not its value in case of class types.

Try final:

final String b = a + "y";

or use equals():

System.out.println(b.equals(c));

Edited:

String literals are directly available in string pool it is not same in this case String b = a + "y", here its in heap. You can use intern() to make it available in string pool.

String b = (a + "y").intern();
System.out.println(a==b);//true
  • My Question is - why instance is not same in the second scenario. When creating a new String, It always checks whether the same string is available in the pool or not. Then after creating String b i.e. "xy" is available in the pool. So when I'm trying to create String c with the same "xy", it should not create new instance.. right?? – user2590663 Apr 06 '15 at 05:07
  • @user2590663 Wrong. You have this notion that "When creating a new String, It always checks whether the same string is available in the pool or not". That simply is not true. The program does not check when it's running. (It would take too much time.) – ajb Apr 06 '15 at 06:05
-1

This might work, and the explanation for why can be found here.

String a = "x";
String b = a + "y";
String c = "xy";
System.out.println(b.equals(c));
rb612
  • 5,280
  • 3
  • 30
  • 68
  • This is irrelevant. The questioner is specifically asking about why instance equality isn't working. He's not asking about the correct way to compare strings. – ajb Apr 06 '15 at 06:06
  • But strings are immutable objects, which would explain why instance inequality isn't working. If he/she is wondering how to correctly check string equality, this is the way to do it. From my perspective, I saw it as a question to why "xy" stored in one variable is a different object from "xy" stored in another variable. – rb612 Apr 06 '15 at 06:48