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

来源:互联网 发布:淘宝上男装剪标折扣店 编辑:程序博客网 时间:2024/06/06 09:03

内存分析:
这里写图片描述
第一个验证程序:

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”;
这里写图片描述

原创粉丝点击