0
String str = "hello";
    String s = str; 
    System.out.println(str==s); 
    String str2 = new String ("world");
    String s2 = str2;
    System.out.println(str2 == s2); 
    String str3 = str + str2;
    String str4 = "helloworld";
    String str5 = new String ("helloworld");
    System.out.println(str3 ==str4); 
    System.out.println(str3 ==str5);

Output

true true false false

Can anyone explain to me the output of the above program snippet in Java?

  • 1
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Paul Jul 17 '20 at 10:08

1 Answers1

0

Update:

Answering to your doubt in the comment:

Here is a diagram I got from internet:

string heap memory string concatenation

This explains clearly why str3 !=str4 and str3 !=str5 (because the moment you do a concatenation, a new object is created.

And when you do something like String s2 = str2; s2 basically is the same reference to the String object str2. This is true for any object in Java. Luckily strings are immutable, but if str2 was some other mutable object, then if you had made any changes in s2 , str2 would also have changed. object assignment


As @Paul mentioned in the comment, == tests whether they are the same object reference in memory. But .equals(..) tests for value equality i.e to find out if they are logically equal.

First, str==s because you are doing String s = str; i.e s is the reference to the same object as str. So both are equal.

Same with s2.

Now,

String str3 = str + str2;
String str4 = "helloworld";

here str3 and str4 are different object references. So they are not equal.

Same with str3 and str5

Abhinaba Chakraborty
  • 3,488
  • 2
  • 16
  • 37
  • When we initalised str2 with "world" using new keyword, it created "world" in both heap and pool memory. When we assign s2 = str2, then s2 points to the "world" already created in pool (it do not point to world created in heap). But when we are comparing str2 with S2 it returns true. Which means address of both is same. Where I am wrong? – Abhay Gupta Jul 17 '20 at 10:20
  • 1
    @AbhayGupta If you assign `a = b` then the object `b` is referencing, will also be referenced by `a`. Therefore `a == b` will always be true. I'm not sure what makes you think that "_it do not point to world created in heap_". – Ivar Jul 17 '20 at 10:27
  • I have not downvoted your answer... Thanku for your help!! – Abhay Gupta Jul 18 '20 at 14:42
  • @Abhay You are most welcome. If you are clear, could you please accept the answer and close the topic ? – Abhinaba Chakraborty Jul 18 '20 at 15:11
  • I have not downvoted your answer... Thanku for your help!! I want to make one thing clear, if you can help? When we write str3 = str + str2, a new helloworld is created. Now when we write str4 = helloworld, str4 points to helloworld already present in pool (this is what I know, and this is what your first diagram say) then they both should be same . – Abhay Gupta Jul 18 '20 at 16:20
  • 1
    Bro, join this room for more discussion - https://chat.stackoverflow.com/rooms/218116/string-pool-discussion – Abhinaba Chakraborty Jul 18 '20 at 18:46
  • @AbhayGupta I have described in details inside the chat room. – Abhinaba Chakraborty Jul 18 '20 at 19:07
  • @Ivar you may also join and correct me if I am wrong anywhere – Abhinaba Chakraborty Jul 18 '20 at 19:09
  • Thanku!! I was unable to reply in chat room as it require some reputation, and I am new in stack overflow. – Abhay Gupta Jul 20 '20 at 05:58
  • Ohh ok @AbhayGupta . I hope you are clear now – Abhinaba Chakraborty Jul 20 '20 at 06:02
  • 1
    Yes I am very much clear... I had some other doubts which https://stackoverflow.com/questions/50636396/where-is-the-new-object-of-string-created-when-we-concat-using-operator this answer solved for me... – Abhay Gupta Jul 20 '20 at 06:03
  • Cool. I hope you will now rock, if you get this question in interview – Abhinaba Chakraborty Jul 20 '20 at 06:05
  • For sure thanx for your help – Abhay Gupta Jul 20 '20 at 06:27