Why Java is platform independent?

+34 votes
asked Apr 17, 2019 in Computers & Electronics by MickeyAzm158 (300 points)
edited Aug 14, 2019 by Morgenstern
I just started learning Java. The teacher said that Java is a platform independent language. He did also explain it a little bit, though in a language that is totally obscure to me. What is a compiler? What is a machine code? And really, what is “platform” defined in the first place? Is anyone here willing to tell me?

1 Answer

+13 votes
answered Jun 9, 2019 by DongSrp66402 (370 points)
edited Jul 26, 2019
We say Java is a platform independent language in the sense that a program written in Java can be executed on any operating system with any hardware. Now, of course, many programs we are all familiar with, such as Skype or Excel, can be used on multiple operating systems like Windows 8 and OS X. But notice that these programs have different versions for different operating systems, which means that they are actually not platform independent. For programs written in Java, you can directly use them on either Windows or Mac OS or even Linux without any modification. This is the special feature of Java.

The reason why Java is platform independent is that it is structured differently from other programming languages like C or C++. With C, the source code (that is, the original content you write in the C programming environment) must be compiled into the machine code for the computer to execute. This is a very basic and yet crucial point: computers can only execute machine code. The source code that we write in most of the programming languages is Greek to computers, just like machine code is Greek to us. The process of compiling the source code to the machine code is done by something called, well, compiler. Now here comes the second important point: a compiler uses the functions of the current operating system to compile the source code, which means that the compiled machine code can only be executed on this very operating system, thus making the program platform dependent.

For programs written in Java, this process is a bit different. Instead of directly being compiled into the machine code, Java source code is compiled into an intermediate code called byte code. The byte code is not specific to any operating system, in other words, it is platform independent. Now of course, as mentioned just now, for any computer to execute any program, it must be in the form of machine code. This also holds true for Java, but Java programs are executed somehow indirectly, via the so-called Java virtual machine aka JVM. Within JVM, Java byte code is “interpreted” to a sequence of subroutines that are pre-compiled in machine code.

Technically speaking, Java is not really platform independent, since it still requires the platform dependent JVM (different operating systems would require different versions of JVM). But in a relative sense, that is, relative to most other programming languages, source code in Java does not need to undergo the process of “translation” in order to transplant to different platforms, so it could still be justifiably considered as platform independent.
Welcome to Instant Answer, where you can ask questions and receive answers from other members of the community.
...