Java ByteCode算术运算

编程入门 行业动态 更新时间:2024-10-10 09:18:22
本文介绍了Java ByteCode算术运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要为学校项目制作一个简单的编译器,我想生成.class文件,我读取了文件格式,但是为了更好地理解.class文件格式和Java字节码,我有这个类:

I'm going to make a simple compiler for a school project, i want generate .class file, i read the file format but to understand better the .class file format and the java bytecode i have this class:

public class Me { public void myMethod() { int a = 5 * 4 + 3 - 2 + 1 / 7 + 28; } }

使用javap命令,我得到这个(用于"myMethod"):

with javap command i get this(for 'myMethod'):

public void myMethod(); flags: ACC_PUBLIC Code: stack=1, locals=2, args_size=1 0: bipush 49 2: istore_1 3: return LineNumberTable: line 3: 0 line 4: 3 LocalVariableTable: Start Length Slot Name Signature 0 4 0 this LMe; 3 1 1 a I

在这一行:

0: bipush 49

我不明白何时获得该数字(49),我看不到算术运算'5 * 4 + 3 ...'的字节码

I dont understand when we get that number(49), i dont see the byte code for the arithmetic operation '5 * 4 + 3 ...'

推荐答案

我不知道何时获得该数字(49),我看不到算术运算'5 * 4 + 3的字节码

I dont understand when we get that number(49), i dont see the byte code for the arithmetic operation '5 * 4 + 3

字节码编译器正在优化它们.根据JLS定义,该表达式是常量表达式,这意味着允许 java 编译器在编译时对其求值并将其硬编码为课程文件.

The bytecode compiler is optimizing them away. The expression is a constant expression according to the JLS definition, and that means that the java compiler is permitted to evaluate it at compile time and hard-code the resulting value into the class file.

如果要查看表达式评估的字节码是什么样的,则需要使用参数,局部变量等作为表达式中的主要";例如

If you want to see what the bytecodes for expression evaluation look like, you need to use parameters, local variables, etc as the "primaries" in the expression; e.g.

public int myMethod(int a, int b, int c) { return a + b * c / 42; }

更多推荐

Java ByteCode算术运算

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

发布评论

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

>www.elefans.com

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