Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Compiler: how should Java language be mapped to JVM instructions?

The semantics of constructs in Java language are defined by the Java language specification, but they're not in terms of JVM instructions. So, is there possiblity that the specification is not strictly defined to prevent mis-implementation of Java compilers? Or the mapping between Java language and JVM instructions is pretty straightforward, there's no need to worry about this?

like image 617
Dagang Avatar asked Dec 05 '13 16:12

Dagang


People also ask

How Java code is executed in JVM?

In Java, programs are not compiled into executable files; they are compiled into bytecode (as discussed earlier), which the JVM (Java Virtual Machine) then executes at runtime. Java source code is compiled into bytecode when we use the javac compiler. The bytecode gets saved on the disk with the file extension .

What type of instructions is supported by Java Virtual Machine?

A Java Virtual Machine instruction consists of a one-byte opcode specifying the operation to be performed, followed by zero or more operands supplying arguments or data that are used by the operation. Many instructions have no operands and consist only of an opcode.

How does Java compiler and JVM work?

Java Virtual Machine (JVM) is a engine that provides runtime environment to drive the Java Code or applications. It converts Java bytecode into machines language. JVM is a part of Java Runtime Environment (JRE). In other programming languages, the compiler produces machine code for a particular system.

How many instructions does JVM have?

Like all other virtual machines, JVM has an instruction set. There are nearly 255 instructions in this set, however you will not be dealing with all of them. JVM takes "class" files as input. Class files are binary files, so it is not an easy job to write a class file from scratch.


1 Answers

It would defeat much of the point of separating the language from the bytecode for there to be a clear mapping of Java code to JVM instructions. Instead the compiler has the freedom to make determinations of which instructions best represent the provided source, in order to optimize the efficiency of what's actually run.

This does mean it's possible for a compiler to do the wrong thing, however generally speaking you can trust major compilers like Oracle's are doing the right thing; they get tested a lot.

like image 112
dimo414 Avatar answered Oct 05 '22 06:10

dimo414