1、第一个案例
- Java语言规范要求完全相同的字符串字面量,应该包含同样的Unicode字符序列(包含同一份码点序列的常量),并且必须是指向同一个String类实例。
package string; public class StringTest4 {public static void main(String[] args) {System.out.println();System.out.println("1");System.out.println("2");System.out.println("3");System.out.println("4");System.out.println("5");System.out.println("6");System.out.println("7");System.out.println("8");System.out.println("9");System.out.println("10"); // 如下 的字符串 “1” 到 “10” 不会再次被加载System.out.println("1");System.out.println("2");System.out.println("3");System.out.println("4");System.out.println("5");System.out.println("6");System.out.println("7");System.out.println("8");System.out.println("9");System.out.println("10");} }D:\Java\jdk-17\bin\java.exe -agentlib:jdwp=transport=dt_shmem,address=javadebug,suspend=y,server=n -javaagent:C:\Users\dgq\AppData\Local\JetBrains\IntelliJIdea2023.2\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "F:\IdeaProjects\JavaSenior\out\production\jdk8;D:\develop\maven\repository\junit\junit\4.13.1\junit-4.13.1.jar;D:\develop\maven\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar" string.StringTest4 Connected to the target VM, address: 'javadebug', transport: 'shared memory'1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 Disconnected from the target VM, address: 'javadebug', transport: 'shared memory'Process finished with exit code 0
2、第二个案例
package string;public class Memory {public static void main(String[] args) {int i = 1;Object obj = new Object();Memory mem = new Memory();mem.foo(obj);}private void foo(Object param) {String str = param.toString();System.out.println(str);}} D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=35590:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\jdk8;D:\develop\maven\repository\junit\junit\4.13.1\junit-4.13.1.jar;D:\develop\maven\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar string.Memory java.lang.Object@404b9385Process finished with exit code 0