Skip to main content

What makes Java Platform Independent?

Let it be cleared first. What do I mean by Platform? and Why a software should be independent from Platform?

What do I mean by Platform?
In general, To run any kind of softwares need a Device(computer, laptops, smart phones etc.) with architecture(i.e. x86, MIPS etc) and an Operating System(Windows, Linux, mac-OS etc.).
These two things works as a unit to make it possible to run a software.

Why a software should be independent from Platform?
People uses different Devices and Operating Systems according to their need.
If we our programming language develop a software for any specific Platform then our software has limited reachability. I will be reachable only those people who have that Platform. That's why our programming language should allow us to develop a software which run on every platform so our software will be Platform Independent.
Note: The language which develop a Platform Independent Software, is a Platform Independent language.

Now you can say, "Java is a Platform Independent language because It develop softwares which can run on any Platform." that sounds pretty good.
That's why Java has slogan to define Platform Independency "Write Once Run Anywhere"

But How Java does this?

Every Language uses a compiler, which convert source code(our written code) into machine code.
 

Before Java, the languages like c and c++ uses machine specific compiler which converts source into machine specific code


Source code(.c, .cpp, .h)
Preprocessor
Include Header, Expand Macro(i, .ii)
            Compiler(gcc, g++)
Assembly code(.s)
Assembler
Machine code(.o, .obj)
Linker
machine executable file(.exe for windows)


here the compiler(gcc, g++) produce CPU-Architecture dependent Assembly code, then Linker will add some extra code and produce Machine code which also become dependent on Operating System. and now this code became platform dependent.

Java know that if our compiler also produce machine code then story will be same. So It create a compiler(javac) which did not produce machine code, instead of that it produced their own developed code. They call it Byte code. To produce Byte code, they did not take any kind help from CPU and OS that's why Byte code was Platform Independent.

But if Byte code was their own code, it means that Operating System didn't know anything about this code, how OS can execute it?  This code needed an environment for it's execution. To resolve this problem they also develop their own Machine but It was a Software not Hardware, So they called it 'Virtual Machine' and finally it know as Java Virtual Machine(JVM).

JVM provides an environment for execution of Byte code.


Source code(.java)
  Compiler(javac)
Byte code(.class)
JVM execute .class 


Now read this carefully, "Java has one unique compiler and different JVMs." Now, a twist in story here. JVMs are dependent on platform. we need to install different JVM for different platform i.e. JVMs for Windows and Linux are different.


Why JVMs are dependent?
It's very obvious that. Any number of abstraction level you are using in any specific technology. Finally it will run on a platform, on a computer. So at the core level things become Platform Dependent.

You can say, Platform Independency is a just a level of abstraction behind Dependency, for providing convenience to Developers.


It is very easy to install our required JVM. Just go to the site of oracle, there is list of all JVMs as per Platforms, Download and install and start use this. It takes few minutes only.

So Now Let see how Java made us capable of writing Platform Independent code

Write Once
  • Write Once our code(.java)
  • Compile it with javac(.class)
Run Anywhere
  • Go on Windows, install JVM and run it.
  • Go on Linux, install JVM and run it.
  • Go on Mac-OS install JVM and run it

Comments

Post a Comment

Popular posts from this blog

Why "F" and "L" suffix | (10.0F, 10L)

Let us take it this way, We will create their needs. So we will get why they are needed. Try to guess, which functions will be executed in the following program: public class MyClass {     public static void main(String args[]) {         MyClass obj = new MyClass();         obj.fun1(10);     }     void fun1(byte val){         System.out.println(val);     }     void fun1(int val){         System.out.println(val);     }     void fun1(float val){         System.out.println(val);     }     void fun1(long val){         System.out.println(val);     }     } It seems like every method is capable to run this program because 10 is still literal because It has no data type. Before Java, In previous technologies, this scenario gave an ambiguity error. But Java solves this problem by removing the concepts of literals. It means Java provide a data type immediately when these born. So here 10 is no more literal. Java provides Integer data type for it. So now it is of Integer t

Promises and Async-await in depth : Asynchronous Programming in Javascript

Promises and Asynchronous Programming One of the most powerful aspects of JavaScript is how easily it handles asynchronous programming. As a language created for the Web, JavaScript needed to be able to respond to asynchronous user interactions such as clicks and key presses from the beginning. Node.js further popularized asynchronous programming in JavaScript by using callbacks as an alternative to events. As more and more programs started using asynchronous programming, events and callbacks were no longer powerful enough to support everything developers wanted to do.  Promises  are the solution to this problem. Promises are another option for asynchronous programming, and they work like futures and deferreds do in other languages. A promise specifies some code to be executed later (as with events and callbacks) and also explicitly indicates whether the code succeeded or failed at its job. You can chain promises together based on success or failure in ways that make your code easier t

Swagger File | Devise Token Auth

openapi : 3.0.1 info : title : API consumes : - application/json produces : - application/json servers : - url : http://localhost:3000 schemes : - "https" - "http" paths : "/auth" : post : summary : User registration requestBody : content : application/json : schema : $ref : "#/definitions/UserRegistrationParameters" responses : "200" : description : "Valid input" content : application/json : example : status : 'success' data : email : "testuser2@yopmail.com" uid : "testuser2@yopmail.com" first_name : "testuser2" last_name : "lname" role : "ABA Admin" "422" : description : "Invalid input"