作为我的一个学校项目的一部分,我需要深入了解Java字节码。我开始编写简单的程序,并使用javap工具查看生成的字节码,关于*ipush指令,我有一个问题。
当我查看这段代码的字节码时:
我正在获取
当我查看这段代码的字节码时:
public class Main{
public static void main(String []args){
int a;
a=5;
a=a+32765;
}
}
我正在获取
数据
。public class Main
SourceFile: "Main.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #3.#12 // java/lang/Object."<init>":()V
#2 = Class #13 // Main
#3 = Class #14 // java/lang/Object
#4 = Utf8 <init>
#5 = Utf8 ()V
#6 = Utf8 Code
#7 = Utf8 LineNumberTable
#8 = Utf8 main
#9 = Utf8 ([Ljava/lang/String;)V
#10 = Utf8 SourceFile
#11 = Utf8 Main.java
#12 = NameAndType #4:#5 // "<init>":()V
#13 = Utf8 Main
#14 = Utf8 java/lang/Object
{
public Main();
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=1
0: iconst_5
1: istore_1
2: iload_1
3: sipush 32765
6: iadd
7: istore_1
8: return
LineNumberTable:
line 4: 0
line 5: 2
line 6: 8
}
当我将32765替换为32769时,因为它不在short类型的范围内,所以会得到
public class Main
SourceFile: "Main.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #4.#13 // java/lang/Object."<init>":()V
#2 = Integer 32769
#3 = Class #14 // Main
#4 = Class #15 // java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Utf8 Code
#8 = Utf8 LineNumberTable
#9 = Utf8 main
#10 = Utf8 ([Ljava/lang/String;)V
#11 = Utf8 SourceFile
#12 = Utf8 Main.java
#13 = NameAndType #5:#6 // "<init>":()V
#14 = Utf8 Main
#15 = Utf8 java/lang/Object
{
public Main();
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=1
0: iconst_5
1: istore_1
2: iload_1
3: ldc #2 // int 32769
5: iadd
6: istore_1
7: return
LineNumberTable:
line 4: 0
line 5: 2
line 6: 7
}
现在数字已经存储在常量池中。我知道没有指令ipush
可以将整数常量推送到堆栈中,但我想知道为什么没有这样的指令?