Does JVM generate bytecode or run bytecode?

13,925

Solution 1

The Java compiler (javac) turns your human-readable code into bytecode, which is then running in a JVM.

From the oracle docs:

A program has to be converted to a form the Java VM can understand so any computer with a Java VM can interpret and run the program. Compiling a Java program means taking the programmer-readable text in your program file (also called source code) and converting it to bytecodes, which are platform-independent instructions for the Java VM.

Solution 2

  1. JVM = JIT Compiler + Java Interpreter + Garbage Collector
  2. JRE = JVM + Library Classes
  3. JDK = JRE + Development Tool

Sun JVM is written in C and Oracle JVM is written in C++

Java compiler javac converts source code into bytecode. JIT Compiler and Java Interpreter inside JVM convert the bytecode into corresponding machine code.

In java, only the source code(.java files) and bytecodes(.class files) are available. And we can't save the machine codes(.exe files) as because .exe files can only be formed at Run Time and vanished from RAM as soon as the program is executed completely.

In our system both the javac.exe(for compiling java source code, eg: javac HelloWorld.java) and java.exe(for executing java bytecode by JVM, eg: java HelloWorld) are called, which are available in .exe formats only(javac.exe and java.exe). So the Java Compiler javac and JVM were not written in Java.If those were written in Java then those might have been available in javac.class and java.class format.

javac comes under JDK and not under JVM.Remember,JVM only works during Run Time means after the compilation of Source Code into Byte Code..but before that javac compiles the source code into byte code. JVM converts bytecode into corresponding machine code by JIT Compiler and Java Interpreter.

For different Operating Systems different JDK and JRE softwares are available by Oracle Corporation; So both the JVM(coming under JRE) and javac Compiler(coming under JDK) are Platform Dependent.So it is confirmed that javac Compiler and JVM are not written in Java.Because Java language is always Platform Independent.

Solution 3

When you say javac file, the Java Compiler (called javac) will convert your code into an intermediate form (bytecode). It does not convert it directly to machine language, which is platform specific, so that you can give the class files (bytecode) to anyone on any platform. This is how "Write once, Run anywhere" works. Instead of compiling to platform specific machine language, it compiles to a generic bytecode.

When you say java file, the JVM will take the bytecode and convert it to native machine language in chunks "on the fly" (during runtime) and execute them. It does this using a JIT compiler (which might be a source of confusion, since this is NOT the same as javac). By the way, the JDK is not the same as javac. The JDK is an SDK (software development kit) that contains everything in Java, including the JVM and javac.

Solution 4

JVM, depending on platform , convert the byte code to m/c code. More precisely, JIT (just-in-time) compiler inside JVM does this. Byte code is generate by javac.exe. And java.exe converts this byte code to m/c code with the help of jvm.dll (in windows).

Solution 5

JVM runs the bytecode, Java compiler generates it.

However, applications can generate the bytecode while they run, but the generated bytecode is again executed by the JVM.

Share:
13,925
Mohamed Seif
Author by

Mohamed Seif

Updated on June 04, 2022

Comments

  • Mohamed Seif
    Mohamed Seif about 2 years

    I'm little confused here, does the JVM represents the bytecode (generate it) or it's just it load the compiled .class files (bytecode) into memory?! or JVM is just specifications to run the bytecode in a platform independent way?! thank you very much.