Basic Java question: String equality

前端 未结 5 1265
孤独总比滥情好
孤独总比滥情好 2020-12-19 11:53
public class A {

    static String s1 = \"I am A\";

    public static void main(String[] args) {
        String s2 = \"I am A\";
        System.out.println(s1 == s         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-19 12:17

    Because the Java Language Specification says:

    String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are "interned" so as to share unique instances, using the method String.intern.

提交回复
热议问题