What is Java Compiler | How Works It

The Java compiler and Java Virtual Machine (JVM) are the most fundamental components used in Java programming. A Java compiler is a computer program that compiles Java programs (source code).

During the compilation process, it converts (translates) source code written in Java programming language into bytecode, which can then be executed by a Java Virtual Machine (JVM).

In other words, the compiler converts source code (.java file) into bytecode (.class file) during the compilation process. The most widely used Java compiler is javac, which is included as a core component of the Java Development Kit (JDK). It’s a command-line tool that we use to compile Java programs.

For example:

javac HelloWorld.java

This command generates a HelloWorld.class file that contains Java bytecode.

Common Java Compiler Options


You can invoke the Java compiler using the javac command from the command prompt or terminal. On Windows systems, this command internally maps to javac.exe, while on Linux and macOS it is simply javac. Some of the compiler options are as follows:

  • -help: Displays all available compiler options
  • -version: Displays the Java compiler version
  • -verbose: Outputs detailed compilation messages
  • -deprecation: Shows usage of deprecated (retired) APIs
  • -nowarn: Suppresses all warning messages

The compiler options are case-sensitive. A screenshot of complete Java compiler options is given below:

A complete Java compiler options.

Key Features of Java Compiler


The following are the key features of the Java compiler that you should remember:

1. Platform Independence

  • Java compiler generates bytecode, which is an intermediate binary representation of a Java program.
  • This bytecode is understood and executed by the Java Virtual Machine (JVM) on the underlying operating system.
  • Bytecode is not machine-specific code, which is why Java programs are portable across different platforms.
  • As a result, the same .class file can run on any system that has a compatible JVM installed.

2. Strong Type Checking

  • Java is a statically typed language. The compiler performs strong type checking and catches many errors at compile time, before the program is executed.
  • This helps reduce runtime errors and improves program reliability.
  • The compiler may also generate additional information (such as metadata) required for proper execution and verification by the JVM.

3. Error Detection

  • During the compilation process, the Java compiler detects various types of errors, including:
    • Syntax errors (such as missing semicolons or curly braces)
    • Type mismatch errors
    • Access control violations (such as accessing private members)
  • If errors are found, the compiler generates a list of error messages.
  • The compiler does not produce .class file (bytecode) until all reported compilation errors are rectified.

4. Optimization

  • Modern Java compilers perform certain compile-time optimizations to produce efficient bytecode.
  • Although most optimizations to improve performance are handled by the JVM at runtime using Just-In-Time (JIT), the compiler still plays an important role in generating optimized and well-structured bytecode.

How Does Java Compiler Work?


Let’s look at how Java compilation works step-by-step with the help of below figure.

How Java compiler works

1. Source Code (*.java)

You write your Java program in one or more .java files using a text editor or an Integrated Development Environment (IDE) such as Eclipse, IntelliJ IDEA, or Visual Studio Code.

2. Java Compiler

The compiler (javac) reads the human-readable source code and performs multiple actions in sequence:

  • Lexical analysis
    The compiler reads the source code character by character and breaks it into a stream of meaningful units called tokens (such as keywords, identifiers, operators, and literals).
  • Syntax analysis
    The compiler checks whether the sequence of tokens follows the formal grammar rules of the Java programming language.
  • Semantic analysis
    Once the code is syntactically correct, the compiler performs semantic analysis to ensure the program is logically correct. This includes:

    • Type checking.
    • Ensuring variables and methods are declared before using them.
    • Verifying proper scope and access rules.
  • Optimization
    After successful semantic analysis, the compiler can perform various optimizations to improve the efficiency and performance of the generated code without changing its meaning.

3. Bytecode Generation

The Java compiler translates the optimized intermediate representation of the program into machine-independent bytecode, which is saved to disk as .class files. This bytecode is not easily readable by humans because it is an intermediate representation in a low-level binary format designed for the JVM.

4. Java Virtual Machine (JVM)

At the final stage, the Java Virtual Machine (JVM) loads the .class files, verifies the bytecode for security and correctness, and executes it at runtime on the underlying operating system.

The JVM may also use Just-In-Time (JIT) compilation to convert frequently executed bytecode into native machine code at runtime for improving performance.

Compilation Errors and Recompilation


During the compilation process, if the compiler finds errors in the program, it generates a list of error messages and notifies the programmer about errors.

The programmer needs to rectify all errors occurring in the program and recompile the program. Until all compilation errors are fixed, no new or updated .class (bytecode) files are generated. You can view the Java compilation process in the flowchart diagram below.

Flowchart diagram of Java compilation process


Conclusion

The Java compiler (javac) is a translator program that converts Java source code (.java files) into machine-independent bytecode (.class files). You cannot execute the generated bytecode (.class file) directly. The JVM executes it.

Note that the compiler only generates bytecode and does not execute the program. During compilation, the compiler translates the entire source code into bytecode and reports all syntactic and semantic errors found in the program. It does not detect logical errors in the program.

Compilers are language-specific. Each programming language has its own compiler, so a C++ compiler cannot compile Java source code. We hope you have understood what the Java compiler is and how it works in Java. Stay tuned for the next tutorial, where you will learn about interpreter in Java and how is it different from the compiler.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.