new String("") 如果常量池中没有这个tom字符串对象,那就在堆中新建一个tom字符串对象,同时在常量池中也创建一个值一样的对象。
String a="tom"; // tom是在常量池中// b在堆中 两个对象的完全不相同所以为falseString b = new String("tom");System.out.print("1:");System.out.print(a==b); //falseSystem.out.println();/*String a = new String("a");String b = a.intern();String c ="a";System.out.println(c==b);*/
通过字面量赋值创建字符串(如:String str=”jack”)时,会先在常量池中查找是否存在相同的字符串,若存在,则将栈中的引用直接指向该字符串;若不存在,则在常量池中生成一个字符串,再将栈中的引用指向该字符串。向常量池成一个jack到常量池。
String d1="jack";// 常量池中有了,直接将d2指向常量池中的d1String d2="jack";System.out.print("2:");System.out.print(d1==d2); // trueSystem.out.println();
调用intern() 方法时,intern方法会先去查询常量池中是否有已经存在,如果存在,则返回常量池中的引用
//3.1 调用intern() 方法时,intern方法会先去查询常量池中是否有已经存在,如果存在,则返回常量池中的引用String e1 = new String("java");String e2 = new String("java"); // e1和e2肯定是不相同的,因为都是在堆中新建的对象String e3 = e1.intern(); // e3是常量池中的那个字符串System.out.print("3.1:");System.out.print(e2==e3);System.out.println();
常量字符串和变量使用“+”拼接时或者变量与变量拼接时会调用stringBuilder.append()在堆上创建新的对象,而不会同时在常量池里新建对象
String f0="user";String f1 =f0+"Name";String f2="userName"; //f2首先会看常量池有没有System.out.print("4:");System.out.print(f1==f2);System.out.println();
常量字符串与常量字符串的“+”操作,编译阶段直接会合成为一个字符串。如String str=”Hello”+”World”,在编译阶段会直接合并成语句String str=”HelloWorld”,然后会先去常量池中查找是否存在”HelloWorld”, 如果找到直接引用,找不到创建一个新的然后放一份到字符串常量池。并不会在堆中创建新的字符串对象
public static void main(String[] args) {// 1. new String("tom") 如果常量池中没有这个a字符串对象,那就在堆中新建一个tom字符串对象,同时在常量池中也创建一个值一样的对象String a="tom"; // tom是在常量池中// b在堆中 两个对象的完全不相同所以为falseString b = new String("tom");System.out.print("1:");System.out.print(a==b);System.out.println();// 2.通过字面量赋值创建字符串(如:String str=”jack”)时,会先在常量池中查找是否存在相同的字符串,// 若存在,则将栈中的引用直接指向该字符串;若不存在,则在常量池中生成一个字符串,再将栈中的引用指向该字符串。// 向常量池成一个jack到常量池String d1="jack";// 常量池中有了,直接将d2指向常量池中的d1String d2="jack";System.out.print("2:");System.out.print(d1==d2); // trueSystem.out.println();//3.1 调用intern() 方法时,intern方法会先去查询常量池中是否有已经存在,如果存在,则返回常量池中的引用String e1 = new String("java");String e2 = new String("java"); // e1和e2肯定是不相同的,因为都是在堆中新建的对象String e3 = e1.intern(); // e3是常量池中的那个字符串System.out.print("3.1:");System.out.print(e2==e3);System.out.println();// 3.2String e4 = new String("Good") + new String("Bye");String e5 = "GoodBye";String e6 = e5.intern();System.out.print("3.2:");System.out.print(e4==e6);System.out.print("t");System.out.print(e5==e6);System.out.println();// 4.常量字符串和变量使用“+”拼接时或者变量与变量拼接时会调用stringBuilder.append()在堆上创建新的对象,而不会同时在常量池里新建对象String f0="user";String f1 =f0+"Name";String f2="userName"; //f2首先会看常量池有没有System.out.print("4:");System.out.print(f1==f2);System.out.println();// 5.1常量字符串的“+”操作,编译阶段直接会合成为一个字符串。如String str=”Hello”+”World”,// 在编译阶段会直接合并成语句String str=”HelloWorld”,然后会去常量池中查找是否存在”HelloWorld”,// 如果找到直接引用,找不到创建一个新的然后放一份到字符串常量池。String g1="Hello"+"World"; // 放入一份“HelloWorld”到常量池String g2="HelloWorld"; // 直接指向常量池中的那份“HelloWorld”System.out.print("5.1:");System.out.print(g1==g2);System.out.println();// 5.2final String g3="See";final String g4="You";String g5=g3+g4;String g6="SeeYou";System.out.print("5.2:");System.out.print(g1==g2);System.out.println();}
参考:《深入java虚拟机3》
本文发布于:2024-02-02 03:25:00,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170681677341073.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |