I'm little confused. Does the JVM generate the bytecode? Or does it just load the compiled .class
files (bytecode) into memory? Or is the JVM just specifications to run the bytecode in a platform-independent way?
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.
- JVM = JIT Compiler + Java Interpreter + Garbage Collector
- JRE = JVM + Library Classes
- 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.
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.
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.
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.
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).
JDK(javac) generates the byte code(.class files). Now this byte code can be run on any platform by the JVM of that platform.
In short Java Virtual Machine runs / interprets / translates Bytecode into native machine code. It does not generate the bytecode. And we can consider JVM as an interpreter.
Below will help to understand the above,
Bytecode, also termed portable code or p-code, is a form of instruction set designed for efficient execution by a software interpreter. It is something in between a human readable source code and a machine readable machine code. A bytecode program may be executed by parsing and directly executing the instructions, one at a time. This kind of bytecode interpreter is very portable. Some systems, called dynamic translators, or just-in-time (JIT) compilers, translate bytecode into machine code as necessary at runtime. This makes the virtual machine hardware-specific, but doesn't lose the portability of the bytecode. Refer this.
A compiler is computer software that transforms computer code written in one programming language (the source language) into another programming language (the target language). A Java compiler is a compiler for the programming language Java. The most common form of output from a Java compiler is Java class files containing platform-neutral Java bytecode.
For Java it has a dedicated compiler called Javac. Javac Compiles your Java code into Bytecode. And then we have the JVM which uses that bytecode.
enter image description hereThere is an inbuilt tool now available called Java Visual VM(after JDK version 6, update 7) which you will find in bin, the directory where javac.exe exists. Just open this tool which monitors processes that use JVM (profiler). It will show javac,eclipse etc as a Java process running on JVM if you execute it while Visual VM is open.
Jdk generates the byte code for a given file with .java extension. JVM converts the byte code into machine language and then executes it.
© 2022 - 2024 — McMap. All rights reserved.