详解String类中的intern()方法

我们用一个经典的例子来理解

package com.jvm.heap; public class MyTest { public static void main(String[] args) { String str1 = new StringBuilder("计算机").append("软件").toString(); System.out.println(str1.intern() == str1); String str2 = new StringBuilder("ja").append("va").toString(); System.out.println(str2.intern() == str2); String str3 = new StringBuilder("1.8.0_111").toString(); System.out.println(str3.intern() == str3); String str4 = new StringBuilder("ja").append("ee").toString(); System.out.println(str4.intern() == str4); } }

true
false
false
true

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspfpg.html