In this post i will tell you something about compilation which is a task of compiler and on other side interpretation which is the task of interpreter.
There are few queries related to this topic which i am going to answer in this post and will try to make you more clear about this topic. Some questions are as follows :-
Why compilation and what is the need of compilation ?
Why interpretation, what is the need of interpretation and who is responsible for it ?
Why interpretation followed by compilation in java why not the vice-versa ?
What is the benefit of introducing both compilation and interpretation in java?
Compilation is the fist step while executing the java program. Java compiler (javac) is responsible for this.Like all other programming languages here in java also compilation does the same thing , simply checking for the syntax error( any error related to the definition and declaration of variables. method,classes and so on). In other word we can say compiler is not involve in any issue related to memory, it just check whether the user have followed the correct sequence and syntax or not. It not then it will compilation error.
javac <file_name> . java
More interestingly in java compiler create a .class file after successful compilation. And .class is created for each and every class defined in the program.
What is .class file and what does it contain?
.class file is created by the compiler and it contain the same code you are written but in optimized way and is is converted into byte code which is JVM readable.
You can also check the content in the .class created by the compiler use a decompiler, which is responsible for decompiling the compiled code so that a user can have a view how compiler is compiling and optimizing the source code. Let me show you the creation of .class file with the help of an example.
import java.lang.Object;
class A{}
class B{}
public class C{
public static void main(String str[]){
System.out.println("main() method in class C");
}
}
Here in the above there are three class A, B and C where A and B is not having and definition and declaration and C has main() method a print statement .
After successful compilation, three .class file will be created like this,
A.class
B.class
C.class
After creating the .class file the interpreter play its role by interpreting .class files.
Here in the above example three .class files are created which are going to be read by the JVM( Java Virtual Machine). For now only remember that JVM is responsible for memory assignment of the declared variables and members in class, details of which we will see in their respective topics.
Interpretation is the 2nd step for the execution of java program, which is always followed by compilation.
Java is responsible for interpreting the .class file. Actually java is formally known as java interpreter and is a executable file means it is present in the JDK in the bin directory named as java.exe.
Interpretation syntax :-
java <class_name>
Must notice that here we can give the name of any class we have defined in our source file. Lets consider the above example with little modification.
import java.lang.Object;
class A{
public static void main(String sush[]) {
System.out.println(" class A main() method ");
}
}
class B{
public static void main(String sush[]) {
System.out.println(" class B main() method ");
}
}
public class C{
public static void main(String str[]){
System.out.println(" class C main() method ");
}
}
Save it as C.java.
Now compile the above code as,
javac C.java because C class is public. Three .class file will be created for each class A, B and C, like this
A.class
B.class
C.class
Now for interpreter there are three option means , it can execute any class. And notice here that all these classes are independent from each other means the respective classes are executed by its name only.
It not like that if you execute by the name of class C and will execute all other classes.
for ex-
java C ( it will execute and print the content of main() method available in class C only)
output:- class C main() methodjava B ( it will execute and print the content of main() method available in class B only)
output:- class B main() method
java A ( it will execute and print the content of main() method available in class B only)
output:- class A main() method
So you have option while interpreting that you can execute any class you want. That is the reason why we are only writing class name for executing our class because JVM while search according the class name which you had compiled.
Need of Interpretation in java ?
Need of interpretation is with memory assignment . After compilation the only job left is to assign memory for the variable defined in the class to assign memory for it and it will be done by JVM only. Which read the .class file and assign the memory for the defined members.
Unless we interpret we are not going to get our desired result on the screen.
Why interpretation followed by compilation in java not the vice-versa ?
It is because to make java as a platform independent language.
After compilation .class files are created and these .class files are portable, that means we can carry these class files and can able to execute in any platform, only condition we need to follow is to must have JVM installed on that platform . This is also a reason why java is called as " write once and run anywhere ".
Compilation must be the first step because it creates .class file and which makes java a platform independent language and defines the potability of code . It is not followed like that all these facilities will get violated.
This is the benefit of introducing compilation and interpretation in java.
ReplyDeletenice article for beginners.thank you.
java tutorial
java tutorial