How is Java platform independent?
Last Updated :
25 Jul, 2024
The meaning of Java platform-independent is that the Java compiled code(byte code) can run on all operating systems. A program is written in a language that is human-readable. It may contain words, phrases, etc. which the machine does not understand. For the source code to be understood by the machine, it needs to be in a language understood by machines, typically a machine-level language. So, here comes the role of a compiler. The compiler converts the high-level language (human language) into a format understood by the machines.
Therefore, a compiler is a program that translates the source code for another program from a programming language into executable code. This executable code may be a sequence of machine instructions that can be executed by the CPU directly, or it may be an intermediate representation that is interpreted by a virtual machine. This intermediate representation in Java is the Java Byte Code.
Step-by-Step Execution of Java Program
- Whenever a program is written in JAVA, the java compiles it.
- The result of the JAVA compiler is the .class file or the bytecode and not the machine's native code (unlike the C compiler).
- The bytecode generated is a non-executable code and needs an interpreter to execute on a machine. This interpreter is the JVM and thus the Bytecode is executed by the JVM.
- And finally, the program runs to give the desired output.
In the case of C or C++ (languages that are not platform independent), the compiler generates an executable file (such as a .exe file) which is both OS-dependent and CPU-dependent. When we try to run this executable file on another OS or CPU architecture, it does not run, since it is specifically compiled for the target operating system and hardware architecture, making it incompatible with others.
Why Java is platform-independent but JVM is platform dependent?
In Java, the main point here is that the JVM depends on the operating system – so if you are running Mac OS X you will have a different JVM than if you are running Windows or some other operating system. This fact can be verified by trying to download the JVM for your particular machine – when trying to download it, you will be given a list of JVM corresponding to different operating systems, and you will obviously pick whichever JVM is targeted for the operating system that you are running. So we can conclude that JVM is platform-dependent and it is the reason why Java is able to become "Platform Independent".
Important Points:
- In the case of Java, it is the magic of Bytecode that makes it platform-independent.
- This adds to an important feature in the JAVA language termed portability. Every system has its own JVM which gets installed automatically when the JDK software is installed. For every operating system separate JVM is available which is capable to read the .class file or byte code.
- An important point to be noted is that while JAVA is a platform-independent language, the JVM is platform-dependent. Different JVM is designed for different OS and byte code is able to run on different OS.
Note: As JVM is platform dependent because of which Java is not considered completely platform-independent.
Similar Reads
What is Java Platform Micro Edition (Java ME)? Java ME was originally developed by Sun Microsystems, and it is now owned and maintained by Oracle Corporation. it is a platform for developing applications for mobile and embedded devices. It is a subset of the Java platform, designed to run on devices with limited memory and processing power. Java
6 min read
What is Core Java? Java is a programming language that is class-based and object-oriented. It is designed to be general-purpose and aims to have fewer implementation dependencies, and serves as a computing platform for application development. However, many new to the software industry often ask what Core Java is. Wha
7 min read
What is Advanced Java? In the realm of coding, creativity, and state-of-the-art technology play a pivotal role in the domain of software creation. Java is known for its platform independence, robustness, and extensive libraries. Advanced Java concepts let you make really complicated programs; they encompass an array of te
13 min read
Difference between Python and Java Programming languages play a fundamental role in computer science and are considered essential for the development of various applications. The two most popular programming languages in recent years have been Python and Java. Both are popular languages with numerous libraries, making it difficult to
4 min read
What is Java Enterprise Edition (Java EE)? In today's world of large-scale applications, businesses need secure, scalable, and efficient software solutions. Java Enterprise Edition (Java EE) now known as Jakarta EE provides a powerful framework for building enterprise-level applications. JEE has become the backbone of many banking, e-commerc
6 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read