Whenever we are asked what makes java different from other programming languages, the first answer that pop-up in our mind is “Java is platform independent”, and Java Virtual Machine is simply held responsible for this. But have we ever thought that what actually was the need of platform independence and how java is platform independent? Or what exactly JVM does? Well, you can find the answers to these questions, below.
A code written in any programming language is very closely bound to the specific hardware and operating system it runs on. For example, a Mac application can’ run on a UNIX workstation. Thus to overcome this problem, major commercial applications like MS-Word or Netscape Navigator have to write almost independently for all platforms they run on. But in case of applications developed using java, we do not have to worry about the platform on which we can run them. This is because of the byte code created by the java compiler.
Let us now have a look on Ordinary compilation as well as Java compilation process:
Ordinary Compilation Process
A code written by the programmer is known as a source code. This source code goes through compilation process and generates a machine code which is different for different machines. The difference in the machine code is due to different compiler for every machine. For example, a Macintosh will have machine code which Windows will not be able to understand. We generally call this machine code as a native executable code.
Java Compilation Process
Unlike ordinary compilers, java’s compiler i.e., javac, does not produce native executable code for a particular machine. Instead, it produces a Byte Code.
Byte code is highly optimized set of instructions designed to run in the Java Runtime Environment (JRE). The vital component of JRE is Java Virtual Machine (JVM) which inspects and executes the byte code. In fact the original JVM was designed as an interpreter for byte code.
Once we write a java source code, the java compiler converts it into byte code, which is same for every machine, be it Macintosh, Windows, UNIX, etc. Thus, only the JVM needs to be implemented for various platforms. Once the run-time package exists for a given system, any java program can run on it.
While executing the byte code on different, various, you may notice that, its execution is a bit slower than that of machine code. This is because JVM first translates the byte code into machine language. Hence, to overcome this problem, JVM takes the help of JIT (Just In Time Compilation). JIT offers caching of the compiled code and thus JVM gets a boost in its performance. Thus, we come to a conclusion that the JVM is a part of the JRE while on the other hand; JIT is a part of the JVM.
An advantage of JVM is that, although its implementation may differ from platform to platform, but the byte code still remains the same for every JVM on different machines. The java virtual machine is an abstract machine designed to be implemented on the top of the existing processors. It hides the underlying operating system from java applications and can be implemented in software or hardware. Also JVM combined with Java Application Programming Interfaces (APIs) make java platform. In fact JVM also helps a java program to be secure, because it is in control and contains the program, thereby preventing the program from generating the side effects outside of the system.
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021