String 常量池
这段时间主要进行一些基础知识的学习,放到这里做一个备忘。
public static void main(String[] args){ String a = "a1"; String b = "a"+ 1; System.out.println(a==b); }//true public static void main(String[] args){ String a = "ab"; String bb = "b"; String b = "a"+ bb; System.out.println(a==b); }//false public static void main(String[] args){ String a = "ab"; final String bb = "b"; String b = "a"+ bb; System.out.println(a==b); }//true public static void main(String[] args){ String a = "ab"; final String bb = getBB(); String b = "a"+ bb; System.out.println(a==b); } private static String getBB(){ return "b"; }//false private static String a = "ab"; public static void main(String[] args){ String s1 = "a"; String s2 = "b"; String s = s1 + s2; System.out.println(s == a);//false System.out.println(s.intern() == a);//true } private static String a = new String("ab"); public static void main(String[] args){ String s1 = "a"; String s2 = "b"; String s = s1 + s2; System.out.println(s == a);//false System.out.println(s.intern() == a);//false System.out.println(s.intern() == a.intern());//false }
关于上面问题的解释,请参照