Java Virtual Machine
Java Virtual Machine (JVM) is an abstract machine that provides a runtime environment for the bytecode to be executed. It is a part of the Java Runtime Environment (JRE) and is the one that calls the main method in the java programme.
The three aspects of JVM are:
1. Specification: JVM is an abstract machine defined by a specification. Java applications can be run only inside the concrete implementation of the abstract specification of JVM.
2. Implementation: The specifications defined in JVM are implemented, and a runtime environment is created for code execution. The implementation of JVM is known as Java Runtime Environment (JRE). JVM implementations are provided by various companies, both open-source and proprietary. HotSpot, the open-source JVM implementation by Oracle, is one of the most popular ones.
3. Instance: Each time we run a java application, e.g., by running the Java command in the command prompt to run the Java class, an instance of JVM is created. This JVM instance runs the entire programme, and it is lost once the programme ends.
The JVM performs the following main tasks:
- Provides runtime environment
- Loads code
- Verifies code
- Executes code
How is Java Platform Independent?
A language is called platform independent if the applications written in that language can run on any operating system (OS). Java, the Python, C#, Scala, etc., are examples of platform-independent programming languages.
In Java, the code is compiled by the compiler and converted into platform-independent bytecode. This byte code can run on any platform, making Java Write Once and Run Anywhere (WORA) language. Hence, java applications can be executed on any platform.
Languages like C++ and C do not generate intermediate code like Java. Instead, they directly generate the machine code, which is specific to one platform. This compiled code of languages can only be understood and executed by the operating system in which it was compiled.
This compiled code cannot run on any other platform, which makes it a platform-dependent language. In the case of Java, the JVM converts the bytecode to a machine code which makes it platform-independent.
Is JVM platform-dependent / platform-independent?
JVM is platform-dependent even though Java is platform-independent. It requires it to be installed in the system before running the java program. When you try downloading JVM for your machine, you will be offered a list of JVMs corresponding to different operating systems.
JVM acts as an interface between the operating system and java language. It provides an environment to execute the .class file. In the end, this depends on the kernel, which is different for different operating systems. Hence, different JVMs are designed for different operating systems.
And as a matter of fact, JVM is a platform-dependent virtual machine that enables Java to be platform-independent.
How is Java portable?
Portability refers to an application’s ability to run on a different platform than it was designed for. Java applications can run on any machine without recompilation or tweaking the code.
Java has a portable bytecode. Once you’ve compiled your Java source to bytecode (.class file), that file can be used on any Java-supported platform without modification. Every platform, whether it is Windows, Linux, etc., can execute the bytecode (.class file) with the help of the JVM. Hence, the bytecode makes Java portable.
Frequently Asked Questions
Why is Java platform-independent and portable?
Java is platform-independent as the Java applications can run on any operating environment. And it is portable as it facilitates us to carry the bytecode to any other platform.
Why is Java called machine independent?
Java applications are not dependent on any property of a particular machine and can be used on any machine. Hence, it is called machine-independent language.
What is meant by platform-independent?
A language is called platform independent if the applications written in that language can run on any operating environment.
What is the difference between platform-dependent and independent?
If a language is platform-dependent, the application will only run on a certain operating system. Eg: C,C++, etc. And if a language is platform-independent, the application will run on diverse operating systems. E.g., Java, Python, etc.
Is C++ platform independent?
No, it’s platform-dependent. The .exe file generated by the compiler is platform-dependent and is not compatible with other Operating Systems.
Conclusion
In this blog, we ran you through the following:
- Features of Java
- What do you mean by platform-independent language?
- How is the Java platform independent?
- What is a Java Virtual Machine?
- Programme execution in Java
- Is the JVM platform-independent/dependent?
- Why is Java portable?
- Some common question related to platform-independent and portability feature