jvm常量池中存放各种变量的简单分析

本文详细解析了Java中常量池的存储原理,探讨了不同类型的变量如何被处理及存储,特别是final修饰符的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * Created by liuzhilei on 2017/2/13.
 */
public class StaticTest {
    /**
     * static会放到方法去,但是不是常量池。常量池在方法区中
     */
    private static int i = 1;

    /**
     * final修饰的对象,即便是基本数据类型的包装类,
     * 都不会放到常量池,因为final修饰的是对象的引用,
     * 只要对象的地址不发生变化,对象本身通过get,set发生变化还是可以的,所以不可能放到常量池
     */
    private final Integer integer = 2;

    /**
     * 常量池中只是保存了对a的变量引用
     */
    private int a = 3;

    //final修饰的基本数据类型,因为已经不可变,所以会放到常量池中
    private final int finalA = 4;

    //String类型的变量本身就是不可变的,所以肯定会放到常量池中
    private String str = "liuzhilei";

}

利用java命令即可分析出来:javap -v StaticTest.class,下面截取的是常量池数据。

Constant pool:
   #1 = Methodref          #10.#31        //  java/lang/Object."<init>":()V
   #2 = Methodref          #32.#33        //  java/lang/Integer.valueOf:(I)Ljava
/lang/Integer;
   #3 = Fieldref           #9.#34         //  com/liu/j2setest/constantspool/Sta
ticTest.integer:Ljava/lang/Integer;
   #4 = Fieldref           #9.#35         //  com/liu/j2setest/constantspool/Sta
ticTest.a:I
   #5 = Fieldref           #9.#36         //  com/liu/j2setest/constantspool/Sta
ticTest.finalA:I
   #6 = String             #37            //  liuzhilei
   #7 = Fieldref           #9.#38         //  com/liu/j2setest/constantspool/Sta
ticTest.str:Ljava/lang/String;
   #8 = Fieldref           #9.#39         //  com/liu/j2setest/constantspool/Sta
ticTest.i:I
   #9 = Class              #40            //  com/liu/j2setest/constantspool/Sta
ticTest
  #10 = Class              #41            //  java/lang/Object
  #11 = Utf8               i //static修饰变量,保存到了方法区,没有存放到常量池,常量池存放i的引用
  #12 = Utf8               I
  #13 = Utf8               integer //final修饰的对象,不会保存到常量池,存放integer的引用
  #14 = Utf8               Ljava/lang/Integer;
  #15 = Utf8               a  //普通变量,常量池保存了对变量a的引用
  #16 = Utf8               finalA
  #17 = Utf8               ConstantValue
  #18 = Integer            4 //final修饰的基本数据类型,值保存到了常量池中
  #19 = Utf8               str
  #20 = Utf8               Ljava/lang/String;
  #21 = Utf8               <init>
  #22 = Utf8               ()V
  #23 = Utf8               Code
  #24 = Utf8               LineNumberTable
  #25 = Utf8               LocalVariableTable
  #26 = Utf8               this
  #27 = Utf8               Lcom/liu/j2setest/constantspool/StaticTest;
  #28 = Utf8               <clinit>
  #29 = Utf8               SourceFile
  #30 = Utf8               StaticTest.java
  #31 = NameAndType        #21:#22        //  "<init>":()V
  #32 = Class              #42            //  java/lang/Integer
  #33 = NameAndType        #43:#44        //  valueOf:(I)Ljava/lang/Integer;
  #34 = NameAndType        #13:#14        //  integer:Ljava/lang/Integer;
  #35 = NameAndType        #15:#12        //  a:I
  #36 = NameAndType        #16:#12        //  finalA:I
  #37 = Utf8               liuzhilei	//String类型本身就是final,所以会保存到常量池
  #38 = NameAndType        #19:#20        //  str:Ljava/lang/String;
  #39 = NameAndType        #11:#12        //  i:I
  #40 = Utf8               com/liu/j2setest/constantspool/StaticTest
  #41 = Utf8               java/lang/Object
  #42 = Utf8               java/lang/Integer
  #43 = Utf8               valueOf
  #44 = Utf8               (I)Ljava/lang/Integer;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值