-2

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";
  • I can find "Tech" in the class file, but it is prefixed with a `\u0001` character. So `"Tech"` is indeed not in the string pool, but `"\u0001Tech"` _is_. This might be Java version dependent though. I am using Java 14. – Sweeper Oct 10 '20 at 08:09
  • 1
    @Sweeper that’s the new string concatenation, introduced with Java 9. See [`StringConcatFactory.makeConcatWithConstants(…)`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/invoke/StringConcatFactory.html#makeConcatWithConstants(java.lang.invoke.MethodHandles.Lookup,java.lang.String,java.lang.invoke.MethodType,java.lang.String,java.lang.Object...)) which mentions the placeholders `\1` and `\2`. – Holger Oct 12 '20 at 14:51

1 Answers1

1

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 "Test.java"
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."<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();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=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();
    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."<init>":(Ljava/lang/String;)V
         9: astore_1
        10: new           #5                  // class 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                  // 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: "Test.java"
zuoqi
  • 26
  • 4