This question has already been answered:

Tell me, how does the compiler work in this case? Interested in how the reference of the object str1 compared with (str2+str3) ? str1 in this case is a reference to a String Object, and what is (str2+str3) ?

 String str1 = "name"; String str2 = "name"; String str3 = ""; System.out.println(str1==(str2+str3) ? "true" : "false"); 

The result will be false

Reported as a duplicate by YurySPb. android Apr 14 '16 at 14:48 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    Operator = in this case compares references to objects, and accordingly str1 and str2+str3 will give false . If you want to get true as equal strings, then use the equals() method, which is redefined in the String class for this case. A nice explanation here.

    • Thanks, I understand how to compare two objects by value. This situation is interested in, the link from str1 will be compared with what? What is (str2 + str3) in this example? - Alexey
    • 2
      As a result of string concatenation, a new object will be created, Java will compare the same object, and when it realizes that it is not, it will return false - Werder
    • Thanks, as an option, you can verify this by comparing through .itern. Those. str1.intern () == (str2 + str3) .intern () - Alexey