|
| 1 | +``` |
| 2 | +layout: post |
| 3 | +title: "Java: The Platform-Independent Programming Language" |
| 4 | +author: gaurav |
| 5 | +categories: [ Java, Core Java ] |
| 6 | +featured: false |
| 7 | +toc: true |
| 8 | +description: "In this article, we will learn about the Java's platform independancy and how java achieves it." |
| 9 | +``` |
| 10 | + |
| 11 | +In this article, we will learn about the Java's platform independency and how java achieves it. |
| 12 | + |
| 13 | +## Introduction |
| 14 | + |
| 15 | +A software programs are very specific to the hardware and operation systems it is written on. When we write a piece of code on a platform, it will compile and run on that sample platform. But for the diversity and portability of the code or software it is important that it should run on other platforms too. |
| 16 | + |
| 17 | +But In practicality their are some limitations to it. Some programming languages are platform dependent that means the code written on the a platform can run on that platform only. For example C. |
| 18 | + |
| 19 | +But Java is an exception to it. |
| 20 | + |
| 21 | +> Java is a platform independent programming language. |
| 22 | +> |
| 23 | +> Java achieves platform independence with the help of **bytecode** and **Java Virtual Machine**. |
| 24 | +
|
| 25 | +Let's see what does it mean by platform independence. |
| 26 | + |
| 27 | +## What is Platform Independence? |
| 28 | + |
| 29 | +If a code written on one platform can run on any other platform, then it is platform independence. |
| 30 | + |
| 31 | +In Java programming language a program written on a platform can be run on any other platform, provided a JVM is installed. |
| 32 | + |
| 33 | +So, in java we can **Write Once Run Anywhere (WORA)**. |
| 34 | + |
| 35 | +## How Java Achieves Platform Independence? |
| 36 | + |
| 37 | +When a developer writes a program it is in human readable format. i.e. `.java` file. |
| 38 | + |
| 39 | +When we compile it, the java compiler converts it to the **bytecode**. i.e. `.class` file. Bytecode is not a native code for machine. It can be understood by JVMs only. |
| 40 | + |
| 41 | +Then J**ava Virtual Machine** (JVM) executes the bytecode. At the end, we can see the output of the program. |
| 42 | + |
| 43 | +Let's take an example to understand the process. Assume, the below code is in a HelloWorld.java file. |
| 44 | + |
| 45 | +```java |
| 46 | +public class HelloWorld { |
| 47 | + public static void main(String[] args) { |
| 48 | + System.out.println("Hello World!"); |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +## Conclusion |
| 56 | + |
| 57 | +Java Programming language is a platform independent. We can **Write Once Run Anywhere (WORA)**. |
| 58 | + |
| 59 | +Java Virtual Machine (JVM) is not platform independent. Different Operating systems have different types of executable instructions. So there are different JVMs for different OS. |
| 60 | + |
| 61 | +All JVMs can understand the bytecode and converts it to respective executable instructions. This makes Java a platform independent language. |
0 commit comments