string内存分析

String s = "abc";

String s1 = "abc";

System.out.println(s== s1);//true:abc可以共享

System.out.println(s.equals(s1));//true

String s2 = new String("abc");//在堆内存开辟空间

String s3 = new String("abc");    

System.out.println(s2== s3);//false,两个对象在堆内存中的地址不一样。

System.out.println(s2.equals(s3));//true。虽然堆内存地址不一样,但是他们指向常量池中的同一字符串常量”abc”,值相同。


相关文章:

  • 2021-08-16
  • 2021-08-09
  • 2021-11-18
  • 2021-12-27
  • 2021-08-23
  • 2022-12-23
猜你喜欢
  • 2021-09-08
  • 2022-02-24
  • 2021-10-24
  • 2021-05-24
  • 2021-12-12
  • 2021-10-26
  • 2021-09-15
相关资源
相似解决方案