Thursday, July 23, 2015

Platform In-dependent Java Codes


Read this blog after understanding what is platform dependency Click here to know about it.
So the problem is understood now. We don't want Native codes that are dependent to O.S. and machines. We want some In-dependent codes. Java was the first language who gave this feature. Lets see through the picture below what Java has done.





Now in this picture the source code is created using Java. And we are analyzing a statement a + b in that code. We have to compile the code first to convert it into an equivalent binary code.

We are having Windows O.S. and Intel processor as our machine's platform. This machine will perform addition when ever 110011 code will be received by the program.

Compiler of java will not consider the underlying instruction set of machine. Rather it will consider Java's own created instruction set. For example - + = 111000 is the code (remember these are not actual instruction codes.). So java compiler rather than writing 110011 in the binary file has written 111000 code. This generated code is named as Byte Code.

Byte code is an intermediate code that can't be understood by any machine directly. Now we need a program that can understand this code. Java comes with a JVM (Java Virtual Machine) that contains an interpreter.

Now JVM knows what is the meaning of 111000 since JVM knows java instruction set. JVM converts the code again for underlying platform at the time of running the program. JVM is not an actual machine but it behaves like it has to do actual processing. So it is named as Virtual Machine.

So consider a situation you are running the Byte Code at Windows and Intel then JVM will convert the Byte Code to windows Native Code on the fly.
If you are running the same Byte Code at Mac and Apple machine then your JVM will convert the Byte Code to Mac's Native Code on the fly.

Hence your program will run on every platform by just compiling once on any platform. But remember, your JVM will be different for different machines.

Now you can give the byte code to any of your clients. They just need the compatible JVM to run your java program. And you will be like:


For understanding the difference between compiler and interpreter click here.

No comments:

Post a Comment