Java字节码研究

本文在本人2个博客https://blog.csdn.net/21aspnethttps://blog.csdn.net/unix21unix21同步发布

1.String和常连池

先上代码:

public class TestApp {

    public static void main(String[] args) {
        
        String s1 = "abc";
        String s2 = new String("abc");
        String s3 = new String("abc");

        System.out.println(s2 == s1.intern());
        System.out.println(s2 == s3.intern());
        System.out.println(s1 == s3.intern());
        System.out.println(s3 == s3.intern());

        String s4 = "abcd";
        String s5 = new String("abcde");
        System.out.println(s4);
        System.out.println(s5.intern());
    }
}

输出:

false
false
true
false
abcd
abcde

第一个知识点--String.intern:

参考:Java-String.intern的深入研究

Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true. All literal strings and string-valued constant expressions are interned. String literals are defined in section 3.10.5 of the The Java? Language Specification.

上面是jdk源码中对intern方法的详细解释。简单来说就是intern用来返回常量池中的某字符串,如果常量池中已经存在该字符串,则直接返回常量池中该对象的引用。否则,在常量池中加入该对象,然后返回引用。

生成class文件

Classfile /D:/TestApp.class
  Last modified 2019-3-5; size 869 bytes
  MD5 checksum 9093744ea00ada929804a84661bb3119
  Compiled from "TestApp.java"
public class TestApp
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #12.#25        // java/lang/Object."<init>":()V
   #2 = String             #26            // abc
   #3 = Class              #27            // java/lang/String
   #4 = Methodref          #3.#28         // java/lang/String."<init>":(Ljava/lang/String;)V
   #5 = Fieldref           #29.#30        // java/lang/System.out:Ljava/io/PrintStream;
   #6 = Methodref          #3.#31         // java/lang/String.intern:()Ljava/lang/String;
   #7 = Methodref          #32.#33        // java/io/PrintStream.println:(Z)V
   #8 = String             #34            // abcd
   #9 = String             #35            // abcde
  #10 = Methodref          #32.#36        // java/io/PrintStream.println:(Ljava/lang/String;)V
  #11 = Class              #37            // TestApp
  #12 = Class              #38            // java/lang/Object
  #13 = Utf8               <init>
  #14 = Utf8               ()V
  #15 = Utf8               Code
  #16 = Utf8               LineNumberTable
  #17 = Utf8               main
  #18 = Utf8               ([Ljava/lang/String;)V
  #19 = Utf8               StackMapTable
  #20 = Class              #39            // "[Ljava/lang/String;"
  #21 = Class              #27            // java/lang/String
  #22 = Class              #40            // java/io/PrintStream
  #23 = Utf8               SourceFile
  #24 = Utf8           
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值