简体   繁体   English

Java虚拟机如何处理常量

[英]How does the Java Virtual Machine deal with constants

So I compiled this code and decompiled it into bytecode: 所以我编译了这段代码并将其反编译成字节码:

System.out.println("hi");

compiles to: 编译为:

getstatic
ldc
invokevirtual

From what I can see here, the literal value "hi" isn't explicitly stored in any bytecode format, so does a .class file also contain a hidden representaion of a stack for that program. 从我在这里看到的,文字值“hi”没有以任何字节码格式显式存储,因此.class文件还包含该程序的堆栈的隐藏表示。 For example when I compiled that program would the .class file contain that bytecode, and a heap data structure containing the literal value "hi". 例如,当我编译该程序时,.class文件将包含该字节码,以及包含文字值“hi”的堆数据结构。 And more generally, does a .class file contain more than just bytecode (eg a representation of the heap, stack and registers). 更一般地说,.class文件包含的不仅仅是字节码(例如堆,堆栈和寄存器的表示)。 thanks! 谢谢!

Constants are stored in seperate parts of class files than bytecode, called Constant Pools. 常量存储在类文件的单独部分中,而不是字节码,称为常量池。 You can find more information about them here: What is the purpose of the Java Constant Pool? 您可以在此处找到有关它们的更多信息: Java Constant Pool的用途是什么?

The class file format is publicly available in Java Virtual Machine Specification 类文件格式在Java虚拟机规范中公开可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM