Basic Java question: String equality

前端 未结 5 1271
孤独总比滥情好
孤独总比滥情好 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:33

    Java manages a String literal pool. It reuses these literals when it can. Therefore the two objects are actually the same String object and == returns true.

    I believe this is called string interning

提交回复
热议问题