If == compares references in Java, why does it evaluate to true with these Strings?

前端 未结 5 1261
余生分开走
余生分开走 2020-12-06 01:27

As it is stated the == operator compares object references to check if they are referring to the same object on a heap. If so why am I getting the \"Equal\" for this piece o

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 02:03

    Comments above have summed it up pretty well.

    I don't have a Java environment handy, but attempting the following should clarify things for you (hopefully this works as I anticipate).

    String str1 = "Str1";  
    String str2 = "Str"; str2 += "1";
    

    Should now print Not equal

提交回复
热议问题