Java字节码的不同行为

编程入门 行业动态 更新时间:2024-10-18 14:16:46
本文介绍了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字节码的不同行为

本文发布于:2023-11-15 23:30:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598926.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字节   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!