Java Compiler
Constructing code may seem to be an important task in programming, and so is the process of compiling too. Interpreting and compiling are inevitable tasks to execute the Java programming language. This article will explain in brief the Java compiler and its uses.
What is a compiler in java?
A compiler is nothing but a computer program that is used for compiling Java programs. It is platform-independent. The primary task is to translate the source code (.java) file into a bytecode(.class) file. Via this process, the compiler generates the bytecode.
A Java compiler consists of Java Programming Language Compiler (javac), and the GNU Compiler for Java (GCJ). The Eclipse Compiler for Java(ECJ) and Jikes.
The main purpose of the compiler is to convert the code written in a high-level language to a machine-level language. It displays the syntaxial errors to the programmers. It is not capable of detecting logical errors.
Activating a Java compiler:
To activate a Java compiler, we can use the “Java.exe” command in the command prompt. It displays all the compiler options as given in the below screenshot.
Here are some of the compiler options:
- -g: It generates all the debugging information
- -nowarn: It generates no warnings.
- -d<directory>: It specifies where to place the generated class files.
- -s<directory>: It mentions where to place the generated source files.
- -depreciation: It identifies the retired APIs.
- -help: It displays all the compiler options.
- -version: It returns the compiler version.
Responsible tasks of a Compiler in Java:
As we already knew, the compiler is used to compile the source program. Here are other primary roles that are taken care of by a compiler.
1. Firstly, the java compiler converts the source code into the bye code by using the Java Virtual Machine, which is also known as JVM.
2. It detects the syntaxial errors and produces a list of all the errors that are found to be in the program code. No object code will be produced until the programmer clears all the displayed errors. We should keep in mind that a compiler will not detect any logical errors. Thus, the logic lies solely in the hands of the programmer.
3. In certain cases, a compiler also adds additional code to the program, if need be.
Working of a Java Compiler:
Before getting into the theoretical context of how a compiler works, take a look at this flowchart:
1. The programmer writes the source code.
2. The next step is to compile this source code. Once the compilation is over, it generates Java bytecode as a .class file.
3. Now, the .class files that are saved on the disk are interpreted and executed by the Java Virtual Machine(JVM) at runtime.
4. During the compilation process, the compiler finds all the possible errors and generates a list of error messages.
5. Once the programmer gets to know about the errors, he corrects the program code accordingly.
6. Once the rectification process is over, the program gets compiled successfully and it generates an object code.
7. Finally, it also generates the output.
8. This is the working of a Java compiler.
Conclusion:
Thus, without the presence of a compiler, the effectiveness of a program is hard to know. This is everything that you need to know about the Java compiler. Along with this, you can also get to know about the functions and workings of an interpreter in our next article.