开发者

Java中String的intern()方法详细说明

String的intern()方法

先看第一个例子

public class HelloWorld {
    public static void main(String[] args) {
        String s1 = new String("hello") + new String("world");
        s1.intern();
        String s2 = "helloworld";
        System.out.println(s1==s2);
    }
}

输出为:true

再看第二个例子

public class HelloWorld {
    public static void main(String[] args) {
        String s1 = new String("hello") + new String("world");
        String s2 = "helloworld";
        s1.intern();
        System.out.println(s1==s2);
    }
}

输出为:false

明明只有一行代码的位置发生了改变,为什么会输出截然不同的结果呢?

下面开始分析。

jdk1.7后的intern方法在调用后,存在两种情况。

一、在调用后发现StringTable中没有所对应的字符串

String s1 = new String("hello") + new String("world");

这句代码会在堆中new一个String对象(其实并没有这么简单,后面讲)

s1.intern();

s1调用了intern方法,然后发现在StringTable中并没有所对应的字符串,那么jvm就会在StringTable中放入一个地址,这个地址指向s1所new的String对象。

String s2 = "helloworld";

jvm发现在StringTable中已经存在指向"helloworld"的地址了,就会把StringTable中的地址给s2。此时s1和s2都指向这个地址。所以是同一个对象。于是返回了true

二、在调用后发现StringTable中存在所对应的字符串

String s1 = new String("hello") + new Stringhttp://www.devze.com("world");

这句代码会在堆中new一个String对象(其实并没有这么简单,后面讲)

String s2 = "helloworld";

jvm在StringTable中存储这一个"helloworld"。

此时s1和s2是两个不同的对象。s1指向堆中,s2指向StringTable。

s1.intern();

jvm发现在StringTable中已经存在"helloworld"了,那么什么都不会做,仅仅是返回这一个字符串。所以结果为false

下面讲一下String s1 = new String("hello") + new String("world"); 底层做了什么。

public class HelloWorld {
    public static void main(String[] args) {
        String s1 = new String("hello") + new String("world");
    }
}

先运行一遍,类加载器加载后,在target中会存在HelloWorld.class

public class HelloWorld {
    public HelloWorld() {
    }

    public static void main(String[] args) {
        String s1 = new String("hello") + new String("world");
    }
}

运行指令Javap -v target/classes/aa/HelloWorld.class进行反编译。 反编译后的详细信息如下:

Classfile /C:/Users/top/Downloads/untitled5/target/classes/aa/HelloWorld.class
  Ljavascriptast modified 2022年4月14日; size 677 bytes
  SHA-256 checksum bf071098fdd9e51e927c011bb021cab16ac1227bd18a6435441f30e30b400f70
  Compiled from "HelloWorld.java"
public class aa.HelloWorld
  minor version: 0
  major version: 52
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #10                         // aa/HelloWorld
  super_class: #11                        // java/lang/Object
  interfaces: 0, fields: 0, methods: 2, attributes: 1
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            // hello
   #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            // world
   #9 = Methodref          #2.#34         // java/lang/StringBuilder.toString:()Ljava/lang/String;
  #10 = Class              #35            // aa/HelloWorld
  #11 = Class              #36            // java/lang/Object
  #12 = Utf8               <init>
  #13 = Ut编程客栈f8               ()V
  #14 = Utf8               Code
  #15 = Utf8               LineNumberTable
  #16 = Utf8               LocalVariableTable
  #17 = Utf8               this
  #18 = Utf8               Laa/HelloWorld;
  #19 = Utf8               main
  #20 = Utf8               ([Ljava/lang/String;)V
  #21 = Utf8               args
  #22 = Utf8               [Ljava/lang/String;
  #23 = Utf8               s1
  #24 = Utf8               Ljava/lang/String;
  #25 = Utf8               SourceFile
  #26 = Utf8               HelloWorld.java
  #27 = NameAndType        #12:#13        // "<init>":()V
  #28 = Utf8               java/lang/StringBuilder
  #29 = Utf8               java/lang/String
  #30 = Utf8               hello
  #31 = NameAndType        #12:#37        // "<init>":(Ljava/lang/String;)V
  #32 = NameAndType        #38:#39        // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #33 = Utf8               world
  #34 = NameAndType        #40:#41        // toString:()Ljava/lang/String;
  #35 = Utf8               aa/HelloWorld
  #36 = Utf8               java/lang/Object
  #37 = Utf8               (Ljava/lang/Sthttp://www.devze.comring;)V
  #38 = Utf8               append
  #39 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
  #40 = Utf8               toString
  #41 = Utf8               ()Ljava/lang/String;
{
  public aa.HelloWorld();
    descri编程客栈ptor: ()V
    flags: (0x0001) 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   Laa/HelloWorld;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: (0x0009) 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 hello
        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 world
        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 5: 0
        line 6: 35
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      36     0  args   [Ljava/lang/String;
           35       1     1    s1   Ljava/lang/String;
}
SourceFile: "HelloWorld.java"

Java中String的intern()方法详细说明

可以看到,

1、jvm先new了一个StringBuilder(非多线程安全),

2、然后new了一个String,

3、调用了StringBuilder的append方法,添加了这一个String对象。

4、再次new一个String对象

5、再次调用了StringBuilder的append方法,添加了这一个String对象。

6、调用StringBuilder的toString方法,将StringBuilder转为String

到此这篇关于Java中String的intern()方法详细说明的文章就介绍到这了,更多相关String的intern()方法内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜