String a = “123”; String a = new String(“123”);

创建String字符串的方式有两种
1.String a = “123”;
2.String a = new String(“123”);

它们大有区别(字符串常量池-为了避免字符串对象过多)

第一种,JVM在创建的时候会去字符串常量池中,判断有没有已经存在的"123"该字符串,有则不创建,直接返回该字符串的引用地址。 没有的话才创建新的,并返回新的引用地址。

第二种:不管任何情况,都直接在堆内存中创建一个新的地址值对象。

   @Test
    public void test06(){
        String a = "123";
        String b ="123";
        String c = new String("123");
        String d = new String("123");
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("System.identityHashCode(a) = " + System.identityHashCode(a));
        System.out.println("System.identityHashCode(b) = " + System.identityHashCode(b));
        System.out.println("System.identityHashCode(c) = " + System.identityHashCode(c));
        System.out.println("System.identityHashCode(d) = " + System.identityHashCode(d));
    }

运行结果

a = 123
b = 123
System.identityHashCode(a) = 758529971
System.identityHashCode(b) = 758529971
System.identityHashCode(c) = 2104457164
System.identityHashCode(d) = 1521118594

直接输出,因为String重写了hashcode和equals方法输出的是值。
a,b地址值相同因为都是字符串常量池中指向同一个地址。
c,d不同因为创建都是在堆内存中新建的对象。

String a = “100” ,String b=“100” , a==b ? true or false

答案肯定是 true 但里面却大有来头。

String a = “100” 是Java中唯一的一种,不用new可以直接创建对象的方式。它会先查看JVM的常量池中是否有"100"该字符串,没有的话就创建并指向该地址值,有的话就直接把指针指向该字符串的地址值。然而 String b = new String(“100”); 该方式创建的对象,会直接在JVM的堆区中,创建一个对象。

回到题目中又有一个疑问:

a==b比较的是什么,a.equals(b)比较的是什么?

答: a==b 比较的是指针指向的地址值是否相同。而equals()是Object类下的一个方法也是比较地址值是否相同,但在String 类中对该方法进行了重写,重写后比较的是值是否相同。

测试下:

 @Test
    public void test08(){
        String a = "100";
        String b = "100";
        String c = new String("100");
        String d = new String("100");

        System.out.println(a==b);  //true   ==比较地址值,相同的值对应JVM常量池中的同一个地址值
        System.out.println(a==c);  //false  ==比较地址值,new创建新的对象
        System.out.println(a==d);  //false  ==比较地址值,new创建新的对象
        System.out.println(c==d);  //false  ==比较地址值,new创建的每一个对象都是新的。
        System.out.println(a.equals(b)); // true   equals()在String类中进行了重写,比较的是值。
        System.out.println(a.equals(c)); //true   equals()在String类中进行了重写,比较的是值。
        System.out.println(a.equals(d)); //true   equals()在String类中进行了重写,比较的是值。
    }
}

结果符合预期
在这里插入图片描述

总结下:
在这里插入图片描述

ps:
System.identityHashCode(a)方法:
无论给定的x对象是否覆盖了hashCode()方法,都会调用默认的hashCode()方法返
回hashCode,如果x == null, 返回0。
这个默认的hashCode()方法就是Object类中的hashCode方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
String a="hello world"; //在java中有一个常量池,当创建String 类型的引用变量给它赋值时,java到它的常量池中找"hello world"是不是在常量池中已存在。如果已经存在则返回这个常量池中的"hello world"的地址(在java中叫引用)给变量a 。注意a并不是一个对象,而是一个引用类型的变量。它里面存的实际上是一个地址值,而这个值是指向一个字符串对象的。在程序中凡是以"hello world"这种常量似的形式给出的都被放在常量池中。 String b=new String("hello world"); //这种用new关键字定义的字符串,是在堆中分配空间的。而分配空间就是由new去完成的,由new去决定分配多大空间,并对空间初始化为字符串"hello world" 返回其在堆上的地址。 通过上面的原理,可以做如下实验: String a="hello world"; String b="hello world"; String c=new String("hello world"); String d=new String("hello world"); if(a==b) System.out.println("a==b"); else System.out.println("a!=b"); if(c==d) System.out.println("c==d"); else System.out.println("c!=d"); //输出结果: a==b c!=d 为什么出现上面的情况呢? String a="hello world"; String b="hello world"; 通过上面的讲解可以知道,a和b都是指向常量池的同一个常量字符串"hello world"的,因此它们返回的地址是相同的。a和b都是引用类型,相当于c语言里面的指针。java里面没有指针的概念,但是实际上引用变量里面放的确实是地址值,只是java为了安全不允许我们对想c语言中的那样对指针进行操作(如++ 、--)等。这样就有效的防止了指针在内存中的游离。 而对于 String c=new String("hello world"); String d=new String("hello world"); 来说是不相等的,他们是有new在堆中开辟了两块内存空间,返回的地址当然是不相等的了。如果我们要比较这两个字符串的内容怎么办呢?可以用下面的语句: if(c.equals(d)) System.out.println("c==d"); else System.out.println("c!=d"); //输出 c==d

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿帆哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值