内存分析:
第一个验证程序:
public class TestString
{
public static void main(String[] args)
{
String a ="hello";
String b ="hello";
String c = new String("hello");
String d = new String("hello");
System.out.print(a==b); // ture
System.out.print(a.equals(b)); // ture
System.out.print(a==c); //false
System.out.print(a.equals(c)); // ture
System.out.print(c==d); //false
System.out.print(c.equals(d)); // ture
}
}
第二个验证程序:
public class TestString
{
public static void main(String[] args)
{
String a = new String("hello");
String b ="hello";
System.out.print(a==b); // false
System.out.print(a.equals(b)); // ture
}
}
第三个验证程序:
public class TestString
{
public static void main(String[] args)
{
String a ="hello";
String b ="world";
String c ="hello" + "world";
String d ="helloworld";
String e = a + b;
System.out.print(c==d); //ture
System.out.print(c==e); //false
}
}
String c =”hello” + “world”; 这条语句
经过编译器优化后,就等于String c =”helloworld”;