Saturday 6 August 2011

How Java achieves platform independence


Java is most widely used application programming language because of its platform independence also called as "write once run anywhere" feature which allows java programs to run any kind of platform, operating system or machine. To understand how Java achieves platform independence it’s important to understand how java works and how java codes gets compiled and executed.


First step is writing java code
For writing java code you can use any text editor including notepad, word pad or edit plus. While writing your java program make sure it must have one public class and one public static void main method which allow it to run as java program when invoked in Java Virtual Machine. After completing coding just saves your file with .java extension and you are ready for next step of compiling your Java program.


Compiling Java code
For compiling java code you must need a java compiler which comes along with Java Development Kit also called as JDK. You can download and install JDK from Sun Microsystems now Oracle's site. Once you are ready with your java compiler you can compile your java code by invoking "javac" command from command line and it will compile your java source file into class file which has .class extension. This Class file is actually java's way to achieve platform independence. This class file contains JVM specific byte code instead of machine code unlike the C or C++ which makes it possible to run java program in any platform.


Running Java application
Now we are ready to run our java application and in the process we will understand important link in java platform independence. We know that after compilation java compiler creates .class file which contains JVM specific byte codes and to execute those byte code we need a piece of software called Java Virtual Machine or JVM. JVM is also known as Java Runtime or JRE. Byte codes of java programs executed inside Java Virtual Machine which is platform dependence, you here it correctly JVM are indeed platform dependent and it has different binary for different platform which you can down load from Sun's site. JVM also comes with JDK or you can download it separately.


It’s important to know that JVM or Java Virtual machine doesn't have any information regarding the programming language. It knows only binary format of the byte code and ensures that the class file generated by java compiler is as per Java Byte Code specification thus eliminating dangers of running a malicious and manipulated java byte codes. We can also generate byte code that adheres to this format in any programming language which makes it easy to write java compiler and java virtual machine in ay programming language.


Since every Java program runs within the boundaries defined by Java Virtual machine or JVM it provides inherent Security. The code run inside the JVM can not go beyond the security constraints defined by the Java Virtual Machine. This is why java application are considered as secure applications over internet and Java applets are most widely used programming platform for internet. Apart from Security Java Virtual machine also provides memory management via Garbage collection which enables java programmer or java developer to focus more on business logic rather than worrying about system aspects like allocating and reclaiming memory.


In conclusion with the use of class file, byte code and Java virtual machine, Java achieves its platform independence and makes development easy for products suppose to run in different platforms.


Refrences:

1.  Java Site from Sun

4 comments:

  1. hey thanx for sharing this,really helped.

    ReplyDelete
  2. Java applications are not platform independent. These applications runs on Java Virtual Machine which converts the java code into byte code. This byte code is platform independent and is understood by JVM of other machine.

    ReplyDelete