Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Java platform-independent when it needs a JVM to run?

I just started learning Java and I'm confused about the topic of platform independence.

Doesn't "independent" imply that Java code should run on any machine and need no special software to be installed? Yet the JVM needs to be present in the machine.

For example, we need to have the Turbo C Compiler in order to compile C/C++ source code and then execute it. The machine has to have the C compiler.

Could somebody please what is meant when Java is described as "platform independent"?

like image 430
Serenity Avatar asked May 01 '10 04:05

Serenity


People also ask

How does JVM make java platform independent?

And the answer would be, it's because of the JVM. The byte code generated by source code compilation would run in any operating system, but the JVM present in a machine differs for each operating system. And this is how java is considered a platform-independent programming language.

Does java need a JVM to run?

To run a Java program, we need JVM because it is the environment on which a bytecode executes. Oracle provides two products: JDK (Java Development Kit) and JRE (Java Runtime Environment). JRE is the basic software that we install to run a Java program.

Why java is platform independent but JDK is dependent?

JVM, JRE and JDK are platform dependent because configuration of each OS differs. But, Java is platform independent. The Java Runtime Environment (JRE) is part of the Java Development Kit (JDK). It contains set of libraries and tools for developing java application.

Is Java compiler independent of platform?

The (implementation of) java compiler is not platform independent. It compiles source code to platform -independent bytecode which is executed by platform-dependent JVM.


2 Answers

Typically, the compiled code is the exact set of instructions the CPU requires to "execute" the program. In Java, the compiled code is an exact set of instructions for a "virtual CPU" which is required to work the same on every physical machine.

So, in a sense, the designers of the Java language decided that the language and the compiled code was going to be platform independent, but since the code eventually has to run on a physical platform, they opted to put all the platform dependent code in the JVM.

This requirement for a JVM is in contrast to your Turbo C example. With Turbo C, the compiler will produce platform dependent code, and there is no need for a JVM work-alike because the compiled Turbo C program can be executed by the CPU directly.

With Java, the CPU executes the JVM, which is platform dependent. This running JVM then executes the Java bytecode which is platform independent, provided that you have a JVM available for it to execute upon. You might say that writing Java code, you don't program for the code to be executed on the physical machine, you write the code to be executed on the Java Virtual Machine.

The only way that all this Java bytecode works on all Java virtual machines is that a rather strict standard has been written for how Java virtual machines work. This means that no matter what physical platform you are using, the part where the Java bytecode interfaces with the JVM is guaranteed to work only one way. Since all the JVMs work exactly the same, the same code works exactly the same everywhere without recompiling. If you can't pass the tests to make sure it's the same, you're not allowed to call your virtual machine a "Java virtual machine".

Of course, there are ways that you can break the portability of a Java program. You could write a program that looks for files only found on one operating system (cmd.exe for example). You could use JNI, which effectively allows you to put compiled C or C++ code into a class. You could use conventions that only work for a certain operating system (like assuming ":" separates directories). But you are guaranteed to never have to recompile your program for a different machine unless you're doing something really special (like JNI).

like image 67
Edwin Buck Avatar answered Oct 20 '22 00:10

Edwin Buck


            Technical Article on How java is platform indepedent? 

Before going into the detail,first you have to understand what is the mean of platform? Platform consists of the computer hardware(mainly architecture of the microprocessor) and OS. Platform=hardware+Operating System

Anything that is platform indepedent can run on any operating system and hardware.

Java is platform indepedent so java can run on any operating system and hardware. Now question is how is it platform independent?

This is because of the magic of Byte Code which is OS indepedent. When java compiler compiles any code then it generates the byte code not the machine native code(unlike C compiler). Now this byte code needs an interpreter to execute on a machine. This interpreter is JVM. So JVM reads that byte code(that is machine indepedent) amd execute it. Different JVM is designed for different OS and byte code is able to run on different OS.

In case of C or C++(language that are not platform indepedent) compiler generate the .exe file that is OS depedent so when we run this .exe file on another OS it will not run because this file is OS depedent so is not compatible with the another OS.

Finally an intermediate OS indepedent Byte code make the java platform independent.

like image 29
Jatin Khurana Avatar answered Oct 20 '22 01:10

Jatin Khurana