Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Java platform independent?

Tags:

java

c++

jvm

When I used C++ programs, I needed Turbo C complier; and when I have a Java program, I need to have JVM. Still C++ isn't platform independent, but Java is! If any Java program require a JVM running in order to execute, why does Java is said to be Platform Independent?

like image 310
user6393697 Avatar asked Mar 25 '26 17:03

user6393697


1 Answers

Java is operating-system independent because it runs on the Java platform (the JVM): The mantra is "write once, run anywhere" because you write your code using the JDK API, compile that once, and it runs on any operating system that has a JVM available. You write your code, wrap it up in a jar, and that jar runs wherever you want to use it, within reasonable bounds. The job of the JDK and JVM are to abstract away the differences in environments.

In contrast, particularly back when Java was created, writing C or C++ for multiple operating systems was a big pain and usually required additional toolkits (of course, the JDK and JVM are a toolkit of sorts), and even today still requires at least recompiling for the target system.

There's nothing magic about Java's OS-independence. It would be entirely possible to build the same thing for C or C++: Compile to an intermediary form, provide a runtime that knows how to interpret or recompile that intermediary form for different environments and provide a library that abstracts away environmental differences. Java just...did that, with its own spin on the language. Later, so did the .Net platform.

like image 107
T.J. Crowder Avatar answered Mar 28 '26 07:03

T.J. Crowder