Saturday, 2 March 2013

How Java actually works?

As stated in earlier post that java in a compiled as well as interpreted language.So now lets look how this works.

To understand this well lets take an example.Lets say you created a file called which has the class Foo in it.When you compile the file using  javac what if formed is another file called Foo.class. Do not worry about the syntax mentioned above at this point.This post is meant only to make you understand how Java works internally. Compilation and execution of Java programs will be dealt in upcoming posts.
     So this Foo.class file created on compilation of is nothing but what we call java bytecode.This bytecode can then be interpreted by Java interpreter present in the JVM.These class files are generally packaged and distributed as jar(Java ARchive) files.

Why do we need this "bytecode"?

     The answer is very simple - platform independence.Lets say you have written a code in C, compiled it on one particular architecture say x86.The program may run fine on this architecture but for other architectures the program may fail.
     In case of java this intermediate code called bytecode  is the result of compilation of java code.This bytecode can then be run on any machine with any architecture. All it needs is a JVM to interpret the bytecode.The Java byte code cannot be directly executed on hardware the way that compiled C code can. Instead the byte code must be interpreted by the JVM (Java Virtual Machine) at runtime in order to be executed.

     Java seeks to find a compromise between a purely compiled language (with no portability) and a purely interpreted language (that is significantly slower). By operating this way, Java gets some of the benefits of compiled languages, while also getting some of the benefits of interpreted languages. However, it also inherits some limitations from both of these languages.

Diagrammatic Representation-

Complete process in a nutshell 


No comments:

Post a Comment

t> UA-39527780-1 back to top