String-intern方法举例分析其含义
之后重新理解这个知识点时,又写了一些小例子
String a1 = new String("str01") ; String a2 = "str01" ; System.out.println(a1 == a2) ;//false
String a1 = new String("str01") ; a1.intern() ; String a2 = "str01" ; System.out.println(a1 == a2) ;//false
String a1 = new String("str") + new String("01") ; String a2 = "str01" ; System.out.println(a1 == a2) ;//fasle
String a1 = new String("str") + new String("01") ; a1.intern() ; String a2 = "str01" ; System.out.println(a1 == a2) ;//true
String s0 = "kvill" ; String s1 = new String("kvill") ; String s2 = new String("kvill") ; System.out.println(s0 == s1);//false s1.intern() ; s2 = s2.intern() ; System.out.println(s0 == s1) ;//false System.out.println(s0 == s1.intern()) ;//true System.out.println(s0 == s2) ;//true
String s1 = new String("kvill") +new String("kvill"); String s2 = s1.intern() ; System.out.println(s2 == s1) ;// true
String s1 = new String("kvill") +new String("kvill"); s1.intern() ; String s2 = "kvillkvill" ; System.out.println(s2 == s1) ;//true