您的位置:首页 > 编程语言 > Java开发

java中两种不同的 string赋值比较

2017-05-26 21:04 555 查看
内存分析:



第一个验证程序:

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
4000
";
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”;

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: