Why Java is Platform Independent?

Image source: TheVerge

Java is an object oriented general purpose programming language and computing platform for developing application. It is similar to C++ but a bit advanced. Java can be downloaded freely and can also be accessed freely. Java was developed by James Gosling with his team members Mike Sheridan and Patrick Naughton. It was developed for Sun Microsystems Company in 1995.It was later conquered by Oracle Corporation.

Java is Concurrent i.e. it can execute many statement at once instead of executing them sequentially. Java is class based and object oriented programming language. It is a platform independent language i.e. the compiled code can be run on any java supporting platform. It runs on the logic of “Write once, run anywhere”.

The platform that helps to run and develop the programs written in java script is called java platform. Java platform consists of execution engine, a compiler, and a set of libraries. Java programs can run on any OS or processor.

Why is JAVA a platform independent language?

Java compiler produces a unique type of code called bytecode unlike c compiler where compiler produces only natively executable code for a particular machine.

When the Java program runs in a particular machine it is sent to java compiler, which converts this code into intermediate code called bytecode. This bytecode is sent to Java virtual machine (JVM) which resides in the RAM of any operating system. JVM recognizes the platform it is on and converts the bytecodes into native machine code. Hence java is called platform independent language.

Detailed Explanation:

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.

Therefore C/C++ programs are said to be platform dependent. The generated .exe files are called as native codes. Native codes are the machine codes that are understood by the machines. For the particular program native code generated is different for each OS. The native code generated by Windows OS differs from native code generated by Unix OS.

Read also: 9 Reasons Why Java is Popular Among Developers and Programmers

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.

The above figure 1 and figure 2 explains clearly about the program execution in C/C++ and JAVA respectively.

Java is an object oriented general purpose programming language and computing platform for developing application. It is similar to C++ but a bit advanced. Java can be downloaded freely and can also be accessed freely. Java was developed by James Gosling with his team members Mike Sheridan and Patrick Naughton. It was developed for Sun Microsystems Company in 1995.It was later conquered by Oracle Corporation.

Java is Concurrent i.e. it can execute many statement at once instead of executing them sequentially. Java is class based and object oriented programming language. It is a platform independent language i.e. the compiled code can be run on any java supporting platform. It runs on the logic of “Write once, run anywhere”.

The platform that helps to run and develop the programs written in java script is called java platform. Java platform consists of execution engine, a compiler, and a set of libraries. Java programs can run on any OS or processor.

Why is JAVA a platform independent language?

Java compiler produces a unique type of code called bytecode unlike c compiler where compiler produces only natively executable code for a particular machine.

When the Java program runs in a particular machine it is sent to java compiler, which converts this code into intermediate code called bytecode. This bytecode is sent to Java virtual machine (JVM) which resides in the RAM of any operating system. JVM recognizes the platform it is on and converts the bytecodes into native machine code. Hence java is called platform independent language.

Detailed Explanation:

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.

Therefore C/C++ programs are said to be platform dependent. The generated .exe files are called as native codes. Native codes are the machine codes that are understood by the machines. For the particular program native code generated is different for each OS. The native code generated by Windows OS differs from native code generated by 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.

The above figure 1 and figure 2 explains clearly about the program execution in C/C++ and JAVA respectively.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store