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

suggestions

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

only large files upload on S3 | Ruby On Rails

models/attachment.rb class Attachment < ApplicationRecord after_initialize :set_storage private def set_storage # larger that 5mb file would be upload on s3 if file . blob . byte_size > 5_000_000 Rails . application . config . active_storage . service = :amazon else Rails . application . config . active_storage . service = :local end end # end of private end

Typecasting | How is Long to Float Conversion possible?

We will take a brief description of Typecasting and will try to do focus on Log to Float Conversion. Typecasting: Assigning a value of one data type to another. When we assign a value of smaller data type to a bigger one. it is called Widening. Java did this conversion automatically as they are compatible. As shown in the following figure: One another kind of conversion, when automatic conversion not possible i.e. when they are not compatible is Shortening. It will be just opposite of above and diagram will be reversed. How is Long to Float Conversion possible? If we look carefully at the diagram, there is one conversion which looks questionable is Long(8 bytes) to Float(4 bytes) conversion. It looks like data lossy conversion. Actually, Type conversion does two things: Either change in range or change in behavior or both. Change in Range: short a = 3456 // this value can be varied within the range of -32768 to 32767 int b = a // now this value can be varied wi