Java常见笔试题目字符串

public class StringTest {
    public static void main(String[] args) {
        String s1 = new String("hello");
        //生成了2个对象(String 池一个,堆一个)
        String s2 = "hello";//指向String 池
        //没有生成对象(因为在String 池里面已经有了一个)
        String s3 = new String("hello");
        //生成了1个对象(String 池一个,堆一个)
        System.out.println(s1==s2);
        System.out.println(s1==s3);
        System.out.println(s2==s3);
    }
}
//输出 false,false,false(因为他们指向的对象都不想等)
public class StringTest {
    public static void main(String[] args) {
        String hello = "hello";
        String lo = "lo";
        System.out.println(hello == "hel" + "lo");
        System.out.println(hello == "hel" + lo);
    }
}
//输出 true,false (第一个字面值)

          

原文地址:https://www.cnblogs.com/guwenren/p/java.html