好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java学习之JVM运行时常量池理解

运行时常量池

运行时常量池是方法区的一部分。Class文件中除了有类的版本、字段、方法、接口等描述信息外,还有一项信息时常量池,用于存放编译期生成的各种字面量和符号引用,这部分内容将在类加载后进入方法区的运行时常量池中存放。

运行时常量池相对于Class文件常量池的另外一个重要特征是具备动态性,Java语言并不要求常量一定只有编译期才能产生,也就是并非预置入Class文件中常量池的内容才能进入方法区运行时常量池,运行时常量池,运行期间也可能将新的常量放入池中,这种特性被开发人员利用比较多的就是String类的intern()方法。

常量池的好处

常量池是为了避免频繁的创建和销毁对象而影响系统性能,其实现了对象的共享。
例如字符串常量池,在编译阶段就把所有的字符串放到一个常量池中。
(1)节省内存空间:常量池中所有相同的字符串常量被合并,只占用一个空间。
(2)节省运行时间:比较字符串时,比equals()快。对于两个引用变量,只用 ""判断引用是否相等,也就可以判断实际值相等。

基本数据类型之间应用双等号,比较的时他们的数值。 复合数据类型之间应用双等号,比较的是他们在内存中的存放地址。

基本类型的包装类和常量池

Java中基本类型的包装类的大部分都实现了常量池技术,即:Byte、Short、Integer、Long、Character、Boolean。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

public class NotInitialzation {

     public static void main(String[] args) {

//        System.out.println(SubClass.value);

//        SubClass sbc = new SubClass();

         Integer inte = 10 ;

         Integer inte1 = 3 + 4 ;

         Integer a = new Integer( 3 );

         Integer b = new Integer( 4 );

         Integer c = a + b;

         Integer d = new Integer( 7 );

         System.out.println(inte1 == c); //true

         System.out.println(c == a + b); //true

         System.out.println(c == d); //false

         System.out.println( "=====================================================" );

         String str = "abcd" ;

         String str1 = "ab" ;

         String str2 = "abcd" + "ab" ;

         String str3 = str + str1;

         String str4 = "abcdab" ;

         System.out.println(str3 == str2); //false

         System.out.println(str2 == str4); //true

 

     }

}

对编译class文件进行反编译:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.

// Jad home page: http://kpdus.tripod测试数据/jad.html

// Decompiler options: packimports(3) fieldsfirst ansi space

// Source File Name:   NotInitialzation.java

import java.io.PrintStream;

public class NotInitialzation

{

     public NotInitialzation()

     {

     }

     public static void main(String args[])

     {

         Integer inte = Integer.valueOf( 10 );

         Integer inte1 = Integer.valueOf( 30 );

         Integer a = new Integer( 3 );

         Integer b = new Integer( 4 );

         Integer c = Integer.valueOf(a.intValue() + b.intValue());

         Integer d = new Integer( 7 );

         System.out.println(c.intValue() == a.intValue() + b.intValue());

         System.out.println(c == d);

         System.out.println( "=====================================================" );

         String str = "abcd" ;

         String str1 = "ab" ;

         String str2 = "abcdab" ;

         String str3 = ( new StringBuilder()).append(str).append(str1).toString();

         String str4 = "abcdab" ;

         System.out.println(str3 == str2);

         System.out.println(str2 == str4);

     }

}

Integer inte = 10;java在编译的时候会直接将代码封装成Integer inte = Integer.valueOf(10);从而使用常量池中的对象。

字面量和符号引用

百度百科解释:在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(notation),几乎所有计算机编程语言都具有对基本值的字面量表示,诸如:整数,浮点数以及字符串;而有很多也对布尔类型的值也支持字面量表示;还有一些甚至对枚举类型的元素以及像数组,记录和对像等复合类型的值也支持字面量表示法;

以上就是java学习之JVM运行时常量池理解的详细内容,更多关于JVM运行时常量池的资料请关注其它相关文章!

原文链接:https://blog.csdn.net/yuming226/article/details/89638489

查看更多关于java学习之JVM运行时常量池理解的详细内容...

  阅读:20次