Hi everyone, welcome back to SrishCodes where I teach you interesting tech concepts that you should know about as a current or aspiring software developer. Today I will be sharing with you about the basics of a Java program, specifically how is it executed. There are no prerequisites for this article.
Execution of a Java Program
As shown in the image above, the steps of executing a Java program are:
Write the source code Xxx.java using a programming text editor (such as Sublime Text, Atom or Notepad++) or an IDE (such as Eclipse or IntelliJ).
Compile the source code Xxx.java into Java portable bytecode Xxx.class using the Java Compiler by issuing command:
javac Xxx.java
This class file can be executes in any platform.
- Run the compiled bytecode Xxx.class with the input to produce the desired output, using the Java Runtime by issuing command:
java Xxx
This translated bytecode into native machine code which machines can execute.
Sample Java Program
/**
* Comment to state the purpose of the program
*/
public class Sample { // Save as "Sample.java" to avoid errors.
public static void main(String[] args) { // Entry point of the program
System.out.println("Hello world!"); // Print 'Hello world!'
}
}
Java Virtual Machine (JVM)
Java Virtual machine (JVM) is the virtual machine that runs the Java bytecode. You get this bytecode by compiling the .java
files into .class
files. .class
files contain the bytecodes understood by the JVM. JVM can be used in two different ways - client and server. Although they are mostly similar, the server VM is generally used for executing long-running server applications which need the fastest possible operating speed.
The JVM is called virtual because it provides a machine interface that does not depend on the underlying operating system. This independence from hardware and the operating system a key feature of the write-once-run-anywhere value of Java programs.
All code assigned to JVM is executed by an execution engine. The execution engine reads the byte code and executes one by one. It uses inbuilt interpreter and JIT compiler to convert the bytecode to machine code and execute it. To improve performance, just-in-time (JIT) compilers interact with the JVM at runtime by taking a block of code (not one statement at a time), optimizes the code, and then translates it to optimized machine code.
There are several implementations of JVM, however the most popular implementation of JVM is provided by Oracle Corporation.
Java Runtime Environment (JRE)
The Java Runtime Environment (JRE) is a software package which bundles JVM, libraries, and several other components to run applications written in the Java. To execute any Java application, you need JRE installed in the machine.
JRE = JVM + libraries to run Java application.
JREs can be downloaded as part of JDKs, or you can download them separately. Unlike JVMs, JREs are platform dependent. For example, you cannot install a 64-bit JRE distribution on 32-bit machine. Similarly, JRE distribution for Windows will not work on a Linux machine; and vice-versa.
Java Development Kit (JDK)
JDKs are a superset of JREs. JDKs contain everything that JREs have, along with development tools for developing, debugging, and monitoring Java applications. You need a JDK in order to develop Java applications.
JDK = JRE + tools to develop Java application
Like JREs, JDKs are also platform dependent. So take note when you download the JDK package for your machine.
Summary
I feel the above image accurately summarizes this article. It is crucial to know the difference between the JVM, JDK and JRE and it is a very common interview question asked at all levels of software engineering.
And that was all for this now! Make sure to follow me and subscribe for the latest posts on more backend development. Until next time