Difference between addition of String Literals and String objects

后端 未结 4 1876
忘掉有多难
忘掉有多难 2021-01-14 10:23

What is the difference between an addition of String Literal and String Object?

For example

    String s1 =\"hello\";
    String s2 =\"hello1\";
             


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-14 11:04

    Because you're comparing references. To compare contents, use s1.equals(s2).

    If your comparison of references is intentional, it is unclear why you're expecting the compiler/JVM to intern or not intern identical strings arising in different ways.

提交回复
热议问题