Java being one of the most popular programming languages, can run on all the major operating systems and also can be used to write native Android apps. But before learning how to start programming with Java, let us discover platform independence and learn the meaning of Java Compiler, JVM, JDK, and JRE.
The Java platform is independent of any particular operating system (OS), making Java programming a platform-independent language.
Java platform consists of the following components:
Let’s consider the following diagram that depicts the relationship between all these components.
So from the above diagram, we can conclude that,
JRE = JVM + library classes
JDK = JRE + Developer tools
The Java compiler (javac) compiles a program written in the Java programming language. Unlike other language compilers, which generate machine code after compilation, the Javac compiler produces a byte code class file.
This class file is now passed to the Java Virtual Machine (JVM), which converts it to an object file containing machine-dependent code. Then this code is executed by the machine.
Let’s discuss this in detail.
Java is a programming language that the Java platform uses. Java is an object-oriented programming language whose syntax is derived from C and OOPS features are derived from C++.
It has its own syntax, rules, and structure, as well as its own programming paradigm.
THE JAVA COMPILER
This is a compiler for the Java programming language and its function is to generate Java class files from the Java source code. A Java class file contains Java byte code that is platform-independent.
JVM loads these class files after generating them and either interprets the byte code or compiles it to machine code using a Just-in-Time (JIT) compiler.
THE JAVA VIRTUAL MACHINE
The Java Virtual Machine (JVM) is a runtime environment for Java code and applications. Java byte code is converted to machine language. The Java Virtual Machine (JVM) is a program or application runtime environment for Java programs. The Java Virtual Machine (JVM) is a component of the Java Run Environment (JRE). Other programming languages' compilers generate machine code for a specific system. The Java compiler, on the other hand, generates code for the Java Virtual Machine.
Here is how JVM works:
First, Java code compiles into bytecode. This bytecode gets interpreted on different machines.
Byte code acts as a bridge between the host system and the Java source code.
The Java Virtual Machine (JVM) is in charge of memory allocation.
THE JAVA RUNTIME ENVIRONMENT (JRE)
JRE is the runtime environment that is required to execute Java programs and applications. To effectively execute Java programs, JRE consists of the Java Virtual Machine (JVM), binaries, and other classes.
JRE is a subset of JDK that lacks development tools such as a Java compiler and debugger. You can install JRE separately if your Java apps were built elsewhere and you simply need to run them on your PC.
You do need to install JDK for this.
You need a JRE installed on your machine as it’s the minimum requirement to run Java programs on your machine.
THE JAVA DEVELOPMENT KIT (JDK)
This is the most essential component of any Java setup. JDK includes the Java Runtime Environment (JRE), as well as the Java compiler, debugger, and other essential classes. JDK is used for Java development since it contains all of the executables, binaries, and tools needed to compile and debug a Java program.
Since JDK is a platform-specific software, we'll have different JDK installers for each OS.
All of these, including the Java language, are parts of the Java platform, which is an environment that enables us to run Java applications.
Hope this helps. That's all for now.