当前位置:网站首页>方法区里面有什么——class文件、class文件常量池、运行时常量池
方法区里面有什么——class文件、class文件常量池、运行时常量池
2022-06-26 09:36:00 【zz好好学java】
最近一直被方法区里面存着什么东西困扰着?
1.方法区里存class文件信息和class文件常量池是个什么关系。
2.class文件常量池和运行时常量池是什么关系。
方法区存着类的信息,常量和静态变量,即类被编译后的数据。这个说法其实是没问题的,只是太笼统了。更加详细一点的说法是方法区里存放着类的版本,字段,方法,接口和常量池。常量池里存储着字面量和符号引用。
符号引用包括:1.类的全限定名,2.字段名和属性,3.方法名和属性。
下面一张图是我画的方法区,class文件信息,class文件常量池和运行时常量池的关系

下面一张图用来表示方法区class文件信息包括哪些内容:

可以看到在方法区里的class文件信息包括:魔数,版本号,常量池,类,父类和接口数组,字段,方法等信息,其实类里面又包括字段和方法的信息。
下面的图表是class文件中存储的数据类型
| 类型 | 名称 | 数量 |
| u4 | magic | 1 |
| u2 | minor_version | 1 |
| u2 | major_version | 1 |
| u2 | constant_pool_count | 1 |
| cp_info | constant_pool | constant_pool_count - 1 |
| u2 | access_flags | 1 |
| u2 | this_class | 1 |
| u2 | super_class | 1 |
| u2 | interfaces_count | 1 |
| u2 | interfaces | interfaces_count |
| u2 | fields_count | 1 |
| field_info | fields | fields_count |
| u2 | methods_count | 1 |
| method_info | methods | methods_count |
| u2 | attribute_count | 1 |
| attribute_info | attributes | attributes_count |
下面用一张图来表示常量池里存储的内容:

用一个class文件实际反编译一下
下面是原java代码
[java] view plain copy
- public class TestInt {
- private String str = "hello";
- void printInt(){
- System.out.println(65535);
- }
- }
经过反编译后获得class文件是下面这样的

可以看出被反编译的class文件中的内容和上面所说的是能对应上的。这就解答了class文件和class文件常量池的关系
class文件常量池和运行时常量池的关系以及区别
class文件常量池存储的是当class文件被java虚拟机加载进来后存放在方法区的一些字面量和符号引用,字面量包括字符串,基本类型的常量。
运行时常量池是当class文件被加载完成后,java虚拟机会将class文件常量池里的内容转移到运行时常量池里,在class文件常量池的符号引用有一部分是会被转变为直接引用的,比如说类的静态方法或私有方法,实例构造方法,父类方法,这是因为这些方法不能被重写其他版本,所以能在加载的时候就可以将符号引用转变为直接引用,而其他的一些方法是在这个方法被第一次调用的时候才会将符号引用转变为直接引用的。
总结:
方法区里存储着class文件的信息和运行时常量池,class文件的信息包括类信息和class文件常量池。
运行时常量池里的内容除了是class文件常量池里的内容外,还将class文件常量池里的符号引用转变为直接引用,而且运行时常量池里的内容是能动态添加的。例如调用String的intern方法就能将string的值添加到String常量池中,这里String常量池是包含在运行时常量池里的,但在jdk1.8后,将String常量池放到了堆中。
边栏推荐
- c语言语法基础之——指针( 多维数组、函数、总结 ) 学习
- Problems encountered by jupyter notebook
- Learning and understanding of thread pool (with code examples)
- Does the go compiled executable have dynamic library links?
- Force buckle ----- remove the maximum and minimum values from the array
- 【LeetCode】59. 螺旋矩阵 II
- The basis of C language grammar -- factoring by function applet
- The basis of C language grammar -- pointer (multidimensional array, function, summary) learning
- Automated testing -- Introduction and use of pytest itself and third-party modules
- Day 3 array, pre post, character space, keyword and address pointer
猜你喜欢

Detailed explanation of the network security competition questions (2) of the 2021 national vocational college skills competition (secondary vocational group)

c语言语法基础之——指针( 多维数组、函数、总结 ) 学习
![Logical English structure [key points]](/img/4b/52a666ed01087adbc5fa4f9e1db393.png)
Logical English structure [key points]

國際化配置

install opencv-contrib-dev to use aruco code

我的创作纪念日

Specific implementation comparison between different programming languages

软件测试---如何选择合适的正交表

测试须知——常见接口协议解析

online trajectory generation
随机推荐
LeetCode 0710. Random numbers in the blacklist - preprocessing implementation o (1) value
Retrofit common request methods and comments, post, get heard file upload
c语言语法基础之——指针( 多维数组、函数、总结 ) 学习
MapReduce & yarn theory
Control setting layout in linear layout_ Gravity doesn't work?
What is the web SSH service port of wgcloud
Redis notes (14) - persistence and data recovery (data persistence RDB and AOF, data recovery, mixed persistence)
Druid data source for background monitoring
Learning and understanding of thread pool (with code examples)
P1296 whispers of cows (quick row + binary search)
mysql学习总结
Notes on sports planning on November 22, 2021
c语言语法基础之——函数 小程序 求阶乘
#云原生征文# 在 Google Kubernetes Cluster 上使用 HANA Expression Database Service
定制拦截器
The basis of C language grammar -- learning of local variables and storage categories, global variables and storage categories, and macro definitions
C中字符串基本操作
SSM项目小例子,SSM整合图文详细教程
自动化测试——关于unitest与pytest初始化共存问题
Use recursion or a while loop to get the name of the parent / child hierarchy