这是一个简单的课程。
public class Test{
private int a = 10;
private float b = 20.0F;
}
使用javap -v -l Test.class
命令查看类文件的结构。在Constant pool
部分中,我应该看到该类的所有常量。不过,我发现浮点值是20.0f,但是找不到int值。
public class Test
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #6.#17 // java/lang/Object."<init>":()V
#2 = Fieldref #5.#18 // Test.a:I
#3 = Float 20.0f
#4 = Fieldref #5.#19 // Test.b:F
#5 = Class #20 // Test
#6 = Class #21 // java/lang/Object
#7 = Utf8 a
#8 = Utf8 I
#9 = Utf8 b
#10 = Utf8 F
#11 = Utf8 <init>
#12 = Utf8 ()V
#13 = Utf8 Code
#14 = Utf8 LineNumberTable
#15 = Utf8 SourceFile
#16 = Utf8 Test.java
#17 = NameAndType #11:#12 // "<init>":()V
#18 = NameAndType #7:#8 // a:I
#19 = NameAndType #9:#10 // b:F
#20 = Utf8 Test
#21 = Utf8 java/lang/Object
{
public Test();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: bipush 10
7: putfield #2 // Field a:I
10: aload_0
11: ldc #3 // float 20.0f
13: putfield #4 // Field b:F
16: return
LineNumberTable:
line 1: 0
line 2: 4
line 3: 10
}
SourceFile: "Test.java"
我应该在常量池中看到CONSTANT_Integer
。
答案 0 :(得分:3)
有一些特殊说明可以加载小整数值(iconst_x
表示值为-1到5,bipush x
表示字节值,sipush x
表示短值。
常量池仅用于此范围之外的整数值,即小于-32768或大于32767的数字。