在`.class`文件中找不到用于连接操作的字符串文字。

huangapple go评论71阅读模式
英文:

String literals in .class file not found for concatenation operation

问题

在进行连接操作之前,是否会在常量池中为 "Tech" 创建一个对象?
我知道对于第一个字符串会创建两个对象。一个在常量池中,另一个在堆区,s1 将引用到堆区的对象。
对于第二个字符串对象,s2 将引用堆区中的对象(连接结果)。
但问题是,是否会在常量池中为字符串 "Tech" 创建一个对象。
请注意,我在某处读到过,在代码中的所有字符串字面量都会在常量池中分配内存,这在编译阶段就会决定。要验证这一点,可以在 .class 文件中找到字符串字面量。但是在编译了下面的代码后,在 .class 文件中并没有找到 "Tech"。

String s1 = new String("ABC");
String s2 = s1 + "Tech";
英文:

Would it create an object in the constant pool for "Tech" before concatenating?
I know for the first String 2 objects would be created. One in the constant pool and another one in the heap area to which s1 would be referencing.
For the second String object, s2 would be referencing to the object(concatenation result) in the heap area.
But the question is, would an object for String "Tech" be created on the constant pool.
Please note I had read it somewhere that all string literals in the code are allocated memory in the constant pool and this is decided during the compilation phase itself. And to verify the same, the string literals can be found in the .class file. But after compiling the below code, "Tech" is not found in the .class file.

String s1 = new String("ABC");
String s2 = s1 + "Tech";

答案1

得分: 1

我的jdk版本是1.8,"Tech"字符串(#31)存在于类文件的常量池中。

Classfile /D:/work/sentinel-1.8.0/Sentinel-1.8.0/rpc/rpc-consumer/target/test-classes/com/cpic/zuoqi/rpc/api/Test.class
  最后修改时间 2020-10-10大小 656 字节
  MD5 校验和 23cf1e4d4960807b50ebc6e180f895ab
  编译自 "Test.java"
public class com.cpic.zuoqi.rpc.api.Test
  次要版本号: 0
  主要版本号: 52
  标志: ACC_PUBLIC, ACC_SUPER
常量池
   #1 = Methodref          #11.#25        // java/lang/Object."<init>":()V
   #2 = Class              #26            // java/lang/String
   #3 = String             #27            // ABC
   #4 = Methodref          #2.#28         // java/lang/String."<init>":(Ljava/lang/String;)V
   #5 = Class              #29            // java/lang/StringBuilder
   #6 = Methodref          #5.#25         // java/lang/StringBuilder."<init>":()V
   #7 = Methodref          #5.#30         // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   #8 = String             #31            // Tech
   #9 = Methodref          #5.#32         // java/lang/StringBuilder.toString:()Ljava/lang/String;
  #10 = Class              #33            // com/cpic/zuoqi/rpc/api/Test
  #11 = Class              #34            // java/lang/Object
  #12 = Utf8               <init>
  #13 = Utf8               ()V
  #14 = Utf8               Code
  #15 = Utf8               LineNumberTable
  #16 = Utf8               LocalVariableTable
  #17 = Utf8               this
  #18 = Utf8               Lcom/cpic/zuoqi/rpc/api/Test;
  #19 = Utf8               test
  #20 = Utf8               s1
  #21 = Utf8               Ljava/lang/String;
  #22 = Utf8               s2
  #23 = Utf8               SourceFile
  #24 = Utf8               Test.java
  #25 = NameAndType        #12:#13        // "<init>":()V
  #26 = Utf8               java/lang/String
  #27 = Utf8               ABC
  #28 = NameAndType        #12:#35        // "<init>":(Ljava/lang/String;)V
  #29 = Utf8               java/lang/StringBuilder
  #30 = NameAndType        #36:#37        // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #31 = Utf8               Tech
  #32 = NameAndType        #38:#39        // toString:()Ljava/lang/String;
  #33 = Utf8               com/cpic/zuoqi/rpc/api/Test
  #34 = Utf8               java/lang/Object
  #35 = Utf8               (Ljava/lang/String;)V
  #36 = Utf8               append
  #37 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
  #38 = Utf8               toString
  #39 = Utf8               ()Ljava/lang/String;
{
  public com.cpic.zuoqi.rpc.api.Test();
    描述符: ()V
    标志: ACC_PUBLIC
    Code:
=1, 局部变量=1, 参数大小=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 3: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/cpic/zuoqi/rpc/api/Test;

  void test();
    描述符: ()V
    标志:
    Code:
=3, 局部变量=3, 参数大小=1
         0: new           #2                  // 类 java/lang/String
         3: dup
         4: ldc           #3                  // 字符串 ABC
         6: invokespecial #4                  // Method java/lang/String."<init>":(Ljava/lang/String;)V
         9: astore_1
        10: new           #5                  // 类 java/lang/StringBuilder
        13: dup
        14: invokespecial #6                  // Method java/lang/StringBuilder."<init>":()V
        17: aload_1
        18: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        21: ldc           #8                  // 字符串 Tech
        23: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        26: invokevirtual #9                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        29: astore_2
        30: return
      LineNumberTable:
        line 5: 0
        line 6: 10
        line 7: 30
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      31     0  this   Lcom/cpic/zuoqi/rpc/api/Test;
           10      21     1    s1   Ljava/lang/String;
           30       1     2    s2   Ljava/lang/String;
}
SourceFile: "Test.java"
英文:

My jdk is 1.8 and "Tech" string(#31) exsit in Constant pool of classFile

Classfile /D:/work/sentinel-1.8.0/Sentinel-1.8.0/rpc/rpc-consumer/target/test-classes/com/cpic/zuoqi/rpc/api/Test.class
Last modified 2020-10-10; size 656 bytes
MD5 checksum 23cf1e4d4960807b50ebc6e180f895ab
Compiled from &quot;Test.java&quot;
public class com.cpic.zuoqi.rpc.api.Test
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref          #11.#25        // java/lang/Object.&quot;&lt;init&gt;&quot;:()V
#2 = Class              #26            // java/lang/String
#3 = String             #27            // ABC
#4 = Methodref          #2.#28         // java/lang/String.&quot;&lt;init&gt;&quot;:(Ljava/lang/String;)V
#5 = Class              #29            // java/lang/StringBuilder
#6 = Methodref          #5.#25         // java/lang/StringBuilder.&quot;&lt;init&gt;&quot;:()V
#7 = Methodref          #5.#30         // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
#8 = String             #31            // Tech
#9 = Methodref          #5.#32         // java/lang/StringBuilder.toString:()Ljava/lang/String;
#10 = Class              #33            // com/cpic/zuoqi/rpc/api/Test
#11 = Class              #34            // java/lang/Object
#12 = Utf8               &lt;init&gt;
#13 = Utf8               ()V
#14 = Utf8               Code
#15 = Utf8               LineNumberTable
#16 = Utf8               LocalVariableTable
#17 = Utf8               this
#18 = Utf8               Lcom/cpic/zuoqi/rpc/api/Test;
#19 = Utf8               test
#20 = Utf8               s1
#21 = Utf8               Ljava/lang/String;
#22 = Utf8               s2
#23 = Utf8               SourceFile
#24 = Utf8               Test.java
#25 = NameAndType        #12:#13        // &quot;&lt;init&gt;&quot;:()V
#26 = Utf8               java/lang/String
#27 = Utf8               ABC
#28 = NameAndType        #12:#35        // &quot;&lt;init&gt;&quot;:(Ljava/lang/String;)V
#29 = Utf8               java/lang/StringBuilder
#30 = NameAndType        #36:#37        // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
#31 = Utf8               Tech
#32 = NameAndType        #38:#39        // toString:()Ljava/lang/String;
#33 = Utf8               com/cpic/zuoqi/rpc/api/Test
#34 = Utf8               java/lang/Object
#35 = Utf8               (Ljava/lang/String;)V
#36 = Utf8               append
#37 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
#38 = Utf8               toString
#39 = Utf8               ()Ljava/lang/String;
{
public com.cpic.zuoqi.rpc.api.Test();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1                  // Method java/lang/Object.&quot;&lt;init&gt;&quot;:()V
4: return
LineNumberTable:
line 3: 0
LocalVariableTable:
Start  Length  Slot  Name   Signature
0       5     0  this   Lcom/cpic/zuoqi/rpc/api/Test;
void test();
descriptor: ()V
flags:
Code:
stack=3, locals=3, args_size=1
0: new           #2                  // class java/lang/String
3: dup
4: ldc           #3                  // String ABC
6: invokespecial #4                  // Method java/lang/String.&quot;&lt;init&gt;&quot;:(Ljava/lang/String;)V
9: astore_1
10: new           #5                  // class java/lang/StringBuilder
13: dup
14: invokespecial #6                  // Method java/lang/StringBuilder.&quot;&lt;init&gt;&quot;:()V
17: aload_1
18: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
21: ldc           #8                  // String Tech
23: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
26: invokevirtual #9                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
29: astore_2
30: return
LineNumberTable:
line 5: 0
line 6: 10
line 7: 30
LocalVariableTable:
Start  Length  Slot  Name   Signature
0      31     0  this   Lcom/cpic/zuoqi/rpc/api/Test;
10      21     1    s1   Ljava/lang/String;
30       1     2    s2   Ljava/lang/String;
}
SourceFile: &quot;Test.java&quot;

huangapple
  • 本文由 发表于 2020年10月10日 15:56:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64291243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定