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

前端 未结 5 1270
余生分开走
余生分开走 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:14

    Java stores all Strings in a string table internally during a run. The references to the two strings are identical because in memory they're stored in the same place. Hence, Equal.

    Your statement is right, that == compares object references. Try the same thing with any other class but Strings and you won't get the same result.

提交回复
热议问题