0
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."<init>":()V
   #2 = Class              #28            // java/lang/StringBuilder
   #3 = Methodref          #2.#27         // java/lang/StringBuilder."<init>":()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               <init>
  #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        // "<init>":()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”字面量在哪?

5个回答

0

已采纳

在jdk底层有很多“java”的字符串. 比如这个

clipboard.png

还是静态常量,jvm启动就直接加载到常量池里了

1

java和八种基本数据类型byte,short,int,long.....等等和其他一些都是默认存在于字符串常量池的,使用javap -v是看不出来的。

0

保留字符串吧

0

1.“Java”在代码运行之前以及存在JVM常量池里了,但是“计算机软件”并没有在常量池中
2."Java".Intern() 是用常量池中的“Java”和你自己代码的“Java”比较 当然是false了

  1. JDK1.7 中 intern()方法返回首次出现的实例引用。
-3

例子也是书上的?

撰写答案

Planets