-3

I have below code, but I don't understand how the output is working.

String s1 = "hello";
String s2 = "hello";

String s3 = new String("hello");
String s4 = new String("hello");

System.out.println(s1==s2);
System.out.println(s3==s4);

The output is true then false. But the hashcode value for all s1,s2,s3 and s4 is same.

Then how one is returning true and another one is returning false. Could you please make me understand this.

Arat Kumar rana
  • 187
  • 1
  • 5
  • 19
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Joe Feb 01 '15 at 13:48
  • 1
    The code provided in the question does not call `hashCode()` even once. – Oleg Estekhin Feb 01 '15 at 13:49
  • My question is why the first println is printing **true** and second one is printing **false** even all have same content? On which basis it is considering the **==**? – Arat Kumar rana Feb 01 '15 at 13:54
  • It seems that your JVM/compiler is optimizing strings on the heap in the first case so that `s1` and `s2` both point to the same content while in the second case you explicitly create a new string and therefore initialize also a new object on the heap. – Roman Vottner Feb 01 '15 at 14:00
  • 1
    @RomanVottner It's the compiler, not the JVM, and the behavior is required by the JLS. – chrylis -cautiouslyoptimistic- Feb 01 '15 at 14:17
  • @chrylis thanks for pointing this out. Haven't gone through the JLS yet to be honest, so it was a guessing - that's why I commented on the post and not answered it :) – Roman Vottner Feb 01 '15 at 14:19

4 Answers4

0

Use s1.equals(s2) for comparison between String, as equals() checks value equality while == checks reference equality, and == would never invoke hashCode().

Your first case is true because literals are interned by the compiler and thus refer to the same object

String s1 = "hello";
String s2 = "hello";
System.out.println(s1==s2); // true
Paul Lo
  • 6,032
  • 6
  • 31
  • 36
0
== tests for reference equality.

.equals() tests for value equality.

and == has nothing to do with hashCode();

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
0

s1 and s2, created using string literal, are always of the same object, being retrieved from the String constant pool. On the other hand, two new String objects are being created for s3 and S4 using new constructor.

Albert Ant
  • 11
  • 3
-1

Paul Lo is right. '==' checks if those objects are stored at the same place in the memory. The equals methode is suppost to return true if the hash is the same, not '=='.

Flo6723
  • 1
  • 1
  • I know if we want to compare the content then, we need to use equals(). But my doubt is why it is returning true and false. – Arat Kumar rana Feb 01 '15 at 13:58
  • Obviosly creates the JVM for your s2 no new object and uses the already existing one. This is possible, because strings are immutable in Java, so s1 == s2 can be true. But it appears that Java does not do the same for s3 and s4. – Flo6723 Feb 01 '15 at 15:45