Java Compiler and Interpreter are the most fundamental tools in Java language that programmers use during programming.
A compiler in Java is a computer program that is used for compiling Java programs. It is platform-independent. It converts (translates) source code (.java file) into bytecode (.class file).
In other words, the compiler (javac.exe) generates bytecode during the compilation process.
A bytecode is a binary code that is understood and interpreted by Java Virtual Machine (JVM) on the underlying operating system. It is not similar to machine code.
It is unreadable by humans because it is composed of numbers that are the only language that computers understand.
Java compiler can be activated by using “Javac.exe” command from the command prompt. Some of the compiler options are as follows:
- -help: Display all the compiler option.
- -version: Returns the compiler version.
- -verbose: Output compiler messages.
- –deprecation: Identify retired APIs.
- –nowarn: Generate no warnings.
The compiler options are case-sensitive. A screenshot of complete Java compiler options is as follows:
Responsible tasks for Compiler
Apart from compiling a source program, Java compiler is responsible for the following tasks that are as follows:
a) Java compiler checks the syntaxial error (Syntax error). It also generates a list of all error messages when it finds errors in the program and does not produce an object code unless the errors are rectified reported by the Java compiler.
b) It converts source code into byte code with the help of Java Virtual Machine (JVM).
c) It also adds the additional code to your program if required.
How does Java compiler work?
We can see in the above figure, where Java source code written by the programmer is compiled by javac and then generates java bytecode (.class file) that is automatically saved on the disk with file extension .class.
This .class file is then interpreted and executed by JVM at runtime on the underlying operating system.
In the compilation process, if the compiler finds errors in the program, then it generates a list of error messages and notifies the programmer about errors.
Then, the programmer needs to rectify all errors occurring in the program and needs to recompile the program. You must recompile the program, otherwise, new corrected object code will not be generated.
You can see the compilation process in the flowchart diagram below.
1. Compiler is a translator program that converts high-level language programs (source code) into the machine language code called object code.
2. We can execute the generated object code at any time.
3. The object code refers to a code that can be executed to generate the output of the program written in the high-level language even without the availability of source code (i.e. actual program).
4. During the compilation process, Java compiler converts the complete program into an object code that is a machine readable and notifies the user about all the errors in the program.
5. Compliers are language specific. That is, each high-level language has its own compiler program. Hence, we cannot use a C++ compiler to compile a source code written in Java.
6. A compiler does not detect logical errors in the program.
7. It only creates an object code and does not execute the program.
Hope that this tutorial has covered all the important points related to Java compiler. I hope that you will have understood the basic points of compiler and its responsible tasks.
Thanks for reading!!!
Next ⇒ Interpreter in Java