Why java is platform independent

First, let’s have a look at how programs are written and executed, how they are executed in C/C++, what is bytecode and native code and finally how java program is executed.

Generally a program will be written in a high level language (i.e. human readable language) which consists of many phrases, words etc that cannot be understood by a machine. Hence it should be converted into machine level language. This work is done by the compiler. The compiler is a program that converts the code from High level language (programming language) to machine level language (Executable code). This executable code can be a sequence of instructions which is directly executed by CPU or can be sent to intermediate Virtual Machine.

When a program is written and compiled in C/C++ language, the code is directly converted into machine readable language .i.e. executable code. This code is generated as .exe file. This generated .exe file can run only on specific operating system. i.e. when the program is compiled in windows OS .exe file can run only in windows OS and not on Unix OS

The Compiling and running of the program process is completely different in JAVA language compared to C/C++ language. Here Bytecodes are produced by an intermediate virtual machine. When the program is compiled by the compiler the intermediate code generated are called Byte codes. Like native codes Byte codes are not executable codes. The virtual machine present in the machine executes these byte codes. These bytecodes can be executed in any other platform irrespective of their generated platform.

When a program is written and compiled in JAVA a separate file is created for a compiled program. This file (.class) is called bytecode in java. The .class file created cannot be executed directly. It does not include executable codes. Instead it will be converted into executable code by a virtual machine in the system. These bytecodes generated by the compiled program are to achieve the purpose of platform independency. Byte code generated in a particular platform can be executed in any other platform i.e. the byte code generated in windows OS can also be executed in Unix OS. The one which makes this possible is the JVM (JAVA VIRTUAL MACHINE). When the program is written and compiled the compiler send the generated bytecodes to the JVM resent in the machine and this JVM converts the byte codes into native code which is readable by a particular machine. Thus the output is displayed.

Irrespective of the platform the JVM belongs to, the generated bytecode can run on any JVM. The outputs of the bytecode run on any JVM will be the same.

Hence the JAVA is called platform independent language.

Posted on by