I have a snippet of simple Java code:
public static void main(String[] args) { String testStr = "test"; String rst = testStr + 1 + "a" + "pig" + 2; System.out.println(rst); }
Compile it with the Eclipse Java compiler, and examine the bytecode using AsmTools. It shows:
There are three local variables in the method. The argument is in slot 0, and slots 1 and 2 are supposedly used by the code. But I think 2 local variables are just enough — index 0 is the argument anyway, and the code needs only one more variable.
In order to see if my idea is correct, I edited the textual bytecode, reduced the number of local variables to 2, and adjusted some related instructions:
I recompiled it with AsmTools and it works fine!
So why don't Javac or the Eclipse compiler do this kind of optimization to use the minimal local variables?
Disadvantages of Bytecode:It takes more time to run the bytecode that the machine code which is machine specific. It is difficult to use some platform-specific features because java is platform-independent. Mandatory installation of Java interpreter to run the byte code.
Advantages of Bytecode Bytecodes are non-runnable codes that rely on the availability of an interpreter, this is where JVM comes into play. It is a machine-level language code that runs on the JVM. It adds portability to Java which resonates with the saying, “write once, read anywhere”.
Bytecode is essentially the machine level language which runs on the Java Virtual Machine. Whenever a class is loaded, it gets a stream of bytecode per method of the class. Whenever that method is called during the execution of a program, the bytecode for that method gets invoked.
Bytecode is the intermediate representation of a Java program, allowing a JVM to translate a program into machine-level assembly instructions. When a Java program is compiled, bytecode is generated in the form of a . class file.
There are several reasons. First off, it's not necessary for performance. The JVM already optimizes things at runtime, so there's no point in adding redundant complexity to the compiler.
However, another major reason noone has mentioned here is debugging. Making the bytecode as close to the original source as possible makes it a lot easier to debug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With