Java is a popular, object-oriented programming language that is platform-independent. A Java program can nearly be run on any device. This is made possible thanks to the Java virtual machine (JVM).
As developers, we often use high-level languages like Java, PHP, JavaScript, etc., to write our programs.
In computers, the CPU is the component responsible for executing or running instructions we write. Unfortunately, it does not understand any high-level languages.
The only language that the CPU can understand is the
000101110011
11001111011000
010110010110110
As you can see, it is a sequence of zeros and ones which are mechanically executed by a computer.
One other thing you need to know about machine language is that it is tied to the hardware. So, each computer has its own machine language.
Given the complexity of machine language, we build translators that convert programs written in a high-level language to machine language.
Now that we have a good understanding of how a computer executes a program, let’s discuss the Java virtual machine.
The first thing to know is that JVM is a computer (or a machine), but not your typical machine. This computer does not exist as actual hardware, and does not even have an operating system. It is a hypothetical computer platform.
Next, as discussed above, a computer needs a machine language to execute programs, so the JVM also has its own machine language which is called Java bytecode. It is the role of the JVM to convert the bytecode into machine language for the actual computer (or hardware).
The JVM is part of the Java Running Environment (JRE).
Note: The JVM was originally for the Java language, but it has evolved to support many other languages like Scala, Groovy, and Kotlin, mainly.
In short, this is how it works:
.java
) is compiled to Java bytecode program (.class
).So, once a Java program is compiled, it can be run on almost any computer as long as it has a JRE (which also simulates the JVM). This is why the Java slogan is:
Write once, run anywhere (WORA)
Let’s see it in practice. Make sure you have a Java programming environment and that you can access these commands:
javac
(pronounced “java-see”): the Java compilerjava
: the Java bytecode interpreterCreate a file with your preferred code editor and call it HelloWorld.java
. Paste the following code snippet in the newly created file:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Now, you can open your terminal (or cmd
on Windows). Move to your HelloWorld.java
file and type:
javac HelloWorld.java
This command will generate a bytecode file called HelloWorld.class
. To interpret this file, you just need to run:
java HelloWorld
That’s all. You can move HelloWorld.class
onto any computer and execute it as above.
The JVM appeared in 1995. It has introduced two revolutionary concepts:
At the beginning of this shot, we said that Java is a platform-independent language. It’s one of the selling points of Java: it can actually run on any computer.
This is made possible because the compiled Java program is not intended to be directly executed by the actual hardware, but by the virtual computer. So, once a computer has the Java bytecode interpreter, it can run any Java bytecode program, and the same program can be run on any computer that has such an interpreter. Amazing!
Note: While Java is platform-independent, the JVM is not. Each operating system has its own JVM implementation.
The second advantage that you as a Java programmer should rejoice in is how the language manages program memory for you. Inside a running JVM, there’s a process called garbage collection, which is in charge of tracking memory usage. It does so by continuously identifying and eliminating unused memory in Java programs.
Let’s wrap up.