【面试题】Java String常量相等(==)问题

问题

以下三个结果分别输出(true or false)?别小看它,很多程序员因为上面问题出过生产bug

String s3 = "s";
String s4 = "s";
System.out.println(s3==s4);


---
String s5 = "hello";
String s6 = "he"+"llo";
System.out.println(s5==s6);


---
Integer i = 2017;
Integer j = 2017;
System.out.println(i==j);


---
String s1 = new String("s");
String s2 = new String("s");
System.out.println(s1==s2);
System.out.println(s1.intern()==s2.intern());

真正执行结果如下:

String s3 = "s";
String s4 = "s";
System.out.println(s3==s4);   //true


---
String s5 = "hello";
String s6 = "he"+"llo";
System.out.println(s5==s6);   //true


---
Integer i = 2017;
Integer j = 2017;
System.out.println(i==j);   //false


---
String s1 = new String("s");
String s2 = new String("s");
System.out.println(s1==s2);   //false
System.out.println(s1.intern()==s2.intern());   //true

解释

  1. 看看Integer的源代码就知道Integer 把-128-127之间的每个值建立了缓存池,所以Integer i =127,Integer j =127,他们是true,超出就是false。可以参考我的这篇文章: 【Java】Integer变量相等(==)比较问题

  2. String s = “s” 是常量池中创建一个对象”s”,所以是true。而String s = new String(”s”)在堆上面分配内存创建一个String对象,栈放了对象引用。如下图:

但在调用s.intern()方法的时候,会将共享池中的字符串与外部的字符串(s)进行比较,如果共享池存在,返回它,如果不同则将外部字符串放入共享池中,并返回其字符串的引用,这样做的好处就是能够节约空间。

String 的intern()方法的官方解释如下:

/**
  * Returns an interned string equal to this string. The VM maintains an internal set of
  * unique strings. All string literals found in loaded classes'
  * constant pools are automatically interned. Manually-interned strings are only weakly
  * referenced, so calling {@code intern} won't lead to unwanted retention.
  *
  * <p>Interning is typically used because it guarantees that for interned strings
  * {@code a} and {@code b}, {@code a.equals(b)} can be simplified to
  * {@code a == b}. (This is not true of non-interned strings.)
  *
  * <p>Many applications find it simpler and more convenient to use an explicit
  * {@link java.util.HashMap} to implement their own pools.
  */
 public native String intern();

s.intern()作用还是很多,比如for循环创建String对象,因为代码事先不并不知道是否存在”hello”或者其他字符串的实例。这样可以节约很多内存空间。

参考资料:

1、你遇到过哪些质量很高的 Java 面试?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值