JVM

String str =“a“ + “b“ 与 String s

2021-02-24  本文已影响0人  三也视界

今天,又在思考一个常见的问题,那就是字符串创建对象系列。发现了一个很多人都模糊不清的问题,那就是
String str =“a” + “b” 与 Strin str = new String(“c”) + new String(“d”) 把对象创建在了哪里?创建了几个字符串对象?
话不多出,show my code:

String str =“a” + “b”

package com.bestqiang.set;

/**
 * @author BestQiang
 */
public class Test2 {
    public static void main(String[] args) {
        String str = "a" + "b";
    }
}

使用 javap进行解析:

Classfile /D:/workspace_idea/ data_structure/out/production/Container/com/bestqiang/set/Test2.class
  Last modified 2019-6-18; size 446 bytes
  MD5 checksum a78553412dae26fae704645a9bf0b82b
  Compiled from "Test2.java"
public class com.bestqiang.set.Test2
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #4.#20         // java/lang/Object."<init>":()V
   #2 = String             #21            // ab
   #3 = Class              #22            // com/bestqiang/set/Test2
   #4 = Class              #23            // java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = Utf8               Code
   #8 = Utf8               LineNumberTable
   #9 = Utf8               LocalVariableTable
  #10 = Utf8               this
  #11 = Utf8               Lcom/bestqiang/set/Test2;
  #12 = Utf8               main
  #13 = Utf8               ([Ljava/lang/String;)V
  #14 = Utf8               args
  #15 = Utf8               [Ljava/lang/String;
  #16 = Utf8               str
  #17 = Utf8               Ljava/lang/String;
  #18 = Utf8               SourceFile
  #19 = Utf8               Test2.java
  #20 = NameAndType        #5:#6          // "<init>":()V
  #21 = Utf8               ab
  #22 = Utf8               com/bestqiang/set/Test2
  #23 = Utf8               java/lang/Object
{
  public com.bestqiang.set.Test2();
    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 6: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/bestqiang/set/Test2;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=2, args_size=1
         0: ldc           #2                  // String ab
         2: astore_1
         3: return
      LineNumberTable:
        line 8: 0
        line 11: 3
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       4     0  args   [Ljava/lang/String;
            3       1     1   str   Ljava/lang/String;
}
SourceFile: "Test2.java"

Process finished with exit code 0

你认为 String str = “a” + “b”;这是创建了几个对象呢?没错,你没看错,就是只创建了一个对象

String str = new String(“a”) + new String(“b”)

package com.bestqiang.set;

/**
 * @author BestQiang
 */
public class Test2 {
    public static void main(String[] args) {

         String str1 = new String("a") + new String("b");

    }
}

javap:

Classfile /D:/workspace_idea/ data_structure/out/production/Container/com/bestqiang/set/Test2.class
  Last modified 2019-6-18; size 686 bytes
  MD5 checksum 14d133b0ba6751a29914b05da34af598
  Compiled from "Test2.java"
public class com.bestqiang.set.Test2
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #11.#27        // java/lang/Object."<init>":()V
   #2 = Class              #28            // java/lang/StringBuilder
   #3 = Methodref          #2.#27         // java/lang/StringBuilder."<init>":()V
   #4 = Class              #29            // java/lang/String
   #5 = String             #30            // a
   #6 = Methodref          #4.#31         // java/lang/String."<init>":(Ljava/lang/String;)V
   #7 = Methodref          #2.#32         // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   #8 = String             #33            // b
   #9 = Methodref          #2.#34         // java/lang/StringBuilder.toString:()Ljava/lang/String;
  #10 = Class              #35            // com/bestqiang/set/Test2
  #11 = Class              #36            // java/lang/Object
  #12 = Utf8               <init>
  #13 = Utf8               ()V
  #14 = Utf8               Code
  #15 = Utf8               LineNumberTable
  #16 = Utf8               LocalVariableTable
  #17 = Utf8               this
  #18 = Utf8               Lcom/bestqiang/set/Test2;
  #19 = Utf8               main
  #20 = Utf8               ([Ljava/lang/String;)V
  #21 = Utf8               args
  #22 = Utf8               [Ljava/lang/String;
  #23 = Utf8               str1
  #24 = Utf8               Ljava/lang/String;
  #25 = Utf8               SourceFile
  #26 = Utf8               Test2.java
  #27 = NameAndType        #12:#13        // "<init>":()V
  #28 = Utf8               java/lang/StringBuilder
  #29 = Utf8               java/lang/String
  #30 = Utf8               a
  #31 = NameAndType        #12:#37        // "<init>":(Ljava/lang/String;)V
  #32 = NameAndType        #38:#39        // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #33 = Utf8               b
  #34 = NameAndType        #40:#41        // toString:()Ljava/lang/String;
  #35 = Utf8               com/bestqiang/set/Test2
  #36 = Utf8               java/lang/Object
  #37 = Utf8               (Ljava/lang/String;)V
  #38 = Utf8               append
  #39 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
  #40 = Utf8               toString
  #41 = Utf8               ()Ljava/lang/String;
{
  public com.bestqiang.set.Test2();
    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 6: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/bestqiang/set/Test2;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=4, locals=2, args_size=1
         0: new           #2                  // class java/lang/StringBuilder
         3: dup
         4: invokespecial #3                  // Method java/lang/StringBuilder."<init>":()V
         7: new           #4                  // class java/lang/String
        10: dup
        11: ldc           #5                  // String a
        13: invokespecial #6                  // Method java/lang/String."<init>":(Ljava/lang/String;)V
        16: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        19: new           #4                  // class java/lang/String
        22: dup
        23: ldc           #8                  // String b
        25: invokespecial #6                  // Method java/lang/String."<init>":(Ljava/lang/String;)V
        28: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        31: invokevirtual #9                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        34: astore_1
        35: return
      LineNumberTable:
        line 9: 0
        line 11: 35
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      36     0  args   [Ljava/lang/String;
           35       1     1  str1   Ljava/lang/String;
}
SourceFile: "Test2.java"

Process finished with exit code 0

从javap的解析结果可以看出,在常量池中生成了 a 和 b两个字符串对象,并没有ab这个字符串对象。在堆中用两个new创建了两个String对象,然后通过StringBuilder对象调用append方法进行两次字符串的添加操作,最后用StringBuilder的toString方法进行返回了一个堆中的字符串对象。


在这里插入图片描述

所以,Strin str = new String(“a”) + new String(“b”) 在堆中创建了三个字符串对象(a字符串对象,b字符串对象,ab字符串对象),在堆中的常量池中创建了2个字符串对象(a字符串对象,b字符串对象),一共创建了5个字符串对象。

总结:

希望大家能够从上面的分析有所收获,上面的分析如有谬论,请大家指出,共同探究。

上一篇下一篇

猜你喜欢

热点阅读