Java字节码的不同行为 [英] Different behaviour of java bytecode

查看:61
本文介绍了Java字节码的不同行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java字节码中的新手。我通过一些示例了解了字节码,但我陷入了一个示例。

这些是我的Java和字节码文件

  class SimpleAdd {
public static void main(char args []){
int a,b,c,d;
a = 9;
b = 4;
c = 3;
d = a + b + c;
System.out.println(d);
}
}
从 SimpleAdd.java编译
类SimpleAdd扩展了java.lang.Object {
SimpleAdd();
代码:
0:aload_0
1:调用特殊#1; //方法java / lang / Object。< init> :()V
4:返回

public static void main(char []);
代码:
0:bipush 9
2:istore_1
3:iconst_4
4:istore_2
5:iconst_3
6:istore_3
7:iload_1
8:iload_2
9:iadd
10:iload_3
11:iadd
12:istore 4
14:getstatic# 2; //字段java / lang / System.out:Ljava / io / PrintStream;
17:iload 4
19:invokevirtual#3; //方法java / io / PrintStream.println:(I)V
22:返回

}

我只想知道为什么当我们有a = 9

指令时会出现bipush 9,而在其他所有情况下,都有icont。

解决方案

iconst 可以将常量值-1推送到5。这是一个单字节指令。 / p>

bipush 可以在-128到127之间推送常量值。这是一个两字节的指令。



要推送9,您不能使用 iconst 。没有 iconst_9 指令。


I am a newbee in Java Bytecode. I was understanding the bytecode through some examples but I got stuck in an example.
These are my java and bytecode file

class SimpleAdd{
    public static void main(char args[]){
        int a,b,c,d;
        a = 9;
        b = 4;
        c = 3;
        d = a + b + c;
        System.out.println(d);
    }
}  
Compiled from "SimpleAdd.java"
class SimpleAdd extends java.lang.Object{
SimpleAdd();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(char[]);
  Code:
   0:   bipush  9
   2:   istore_1
   3:   iconst_4
   4:   istore_2
   5:   iconst_3
   6:   istore_3
   7:   iload_1
   8:   iload_2
   9:   iadd
   10:  iload_3
   11:  iadd
   12:  istore  4
   14:  getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   17:  iload   4
   19:  invokevirtual   #3; //Method java/io/PrintStream.println:(I)V
   22:  return

}  

I just want to know why there is bipush 9 when we have instruction a = 9
And in all other case there is iconst.

解决方案

iconst can push constant values -1 to 5. It is a single-byte instruction.

bipush can push constant values between -128 and 127. It is a two-byte instruction.

To push 9 you cannot use iconst. There is no iconst_9 instruction.

这篇关于Java字节码的不同行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆