String s1 = new StringBuilder().append("计算机").append("软件").toString();
System.out.println(s1.intern() == s1);
String s2 = new StringBuilder().append("ja").append("va").toString();
System.out.println(s2.intern() == s2);
输出结果
true
false
第一个true我能理解,但是那个为什么第二个是false呢,书里面说“java”在stringbuilder.tostring()之前已经出现过。我javap查看后
Constant pool:
#1 = Methodref #14.#27 // java/lang/Object."":()V
#2 = Class #28 // java/lang/StringBuilder
#3 = Methodref #2.#27 // java/lang/StringBuilder."":()V
#4 = String #29 // 计算机
#5 = Methodref #2.#30 // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
#6 = String #31 // 软件
#7 = Methodref #2.#32 // java/lang/StringBuilder.toString:()Ljava/lang/String;
#8 = Fieldref #33.#34 // java/lang/System.out:Ljava/io/PrintStream;
#9 = Methodref #35.#36 // java/lang/String.intern:()Ljava/lang/String;
#10 = Methodref #37.#38 // java/io/PrintStream.println:(Z)V
#11 = String #39 // ja
#12 = String #40 // va
#13 = Class #41 // TestPool
#14 = Class #42 // java/lang/Object
#15 = Utf8
#16 = Utf8 ()V
#17 = Utf8 Code
#18 = Utf8 LineNumberTable
#19 = Utf8 main
#20 = Utf8 ([Ljava/lang/String;)V
#21 = Utf8 StackMapTable
#22 = Class #43 // "[Ljava/lang/String;"
#23 = Class #44 // java/lang/String
#24 = Class #45 // java/io/PrintStream
#25 = Utf8 SourceFile
#26 = Utf8 TestPool.java
#27 = NameAndType #15:#16 // "":()V
#28 = Utf8 java/lang/StringBuilder
#29 = Utf8 计算机
#30 = NameAndType #46:#47 // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
#31 = Utf8 软件
#32 = NameAndType #48:#49 // toString:()Ljava/lang/String;
#33 = Class #50 // java/lang/System
#34 = NameAndType #51:#52 // out:Ljava/io/PrintStream;
#35 = Class #44 // java/lang/String
#36 = NameAndType #53:#49 // intern:()Ljava/lang/String;
#37 = Class #45 // java/io/PrintStream
#38 = NameAndType #54:#55 // println:(Z)V
#39 = Utf8 ja
#40 = Utf8 va
#41 = Utf8 TestPool
#42 = Utf8 java/lang/Object
#43 = Utf8 [Ljava/lang/String;
#44 = Utf8 java/lang/String
#45 = Utf8 java/io/PrintStream
#46 = Utf8 append
#47 = Utf8 (Ljava/lang/String;)Ljava/lang/StringBuilder;
#48 = Utf8 toString
#49 = Utf8 ()Ljava/lang/String;
#50 = Utf8 java/lang/System
#51 = Utf8 out
#52 = Utf8 Ljava/io/PrintStream;
#53 = Utf8 intern
#54 = Utf8 println
#55 = Utf8 (Z)V
请问那个最初的“java”字面量在哪?