Java Tutorial



Java Tutorial

From Java Source Code to Executable


Java is a popular programming language known for its versatility and platform independence. One of the key aspects of Java is its compilation process, which transforms human-readable Java source code into executable machine code. In this blog post, we will explore the intricacies of the compilation process in Java, from the initial source code to the final executable. We will also address common questions such as whether Java is a compiled or interpreted language and what Java source code is compiled into. So let's dive into the world of Java compilation!


Java is often described as a "compiled and interpreted" language. This distinction arises from the fact that Java source code undergoes compilation before it can be executed. Unlike languages like C or C++, where compilation results in machine code that is directly executed, Java takes an additional step by transforming the source code into bytecode. This bytecode is a low-level representation of the code that can be interpreted and executed by the Java Virtual Machine (JVM).

Is Java Interpreted or Compiled?

Java is both compiled and interpreted. When you compile a Java source code file (with a .java extension) using the Java compiler (javac), it generates bytecode (with a .class extension) instead of machine code. This bytecode is platform-independent and can be executed on any system with a compatible JVM.

To understand this concept better, let's consider a simple Java program that displays "Hello, World!" on the console:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");

The compilation process in Java translates the source code into bytecode, which allows for portability across different platforms and operating systems. The bytecode is not directly executable by the machine but is interpreted by the JVM at runtime.

The interpretation step involves the JVM executing the bytecode line by line, translating it into machine code specific to the underlying hardware, and executing it. The JVM also performs various runtime optimizations, such as just-in-time (JIT) compilation, which can further enhance the performance of the executed code.

This combination of compilation and interpretation in Java provides benefits such as platform independence, runtime flexibility, and dynamic class loading. It allows Java programs to be written once and run on any system with a compatible JVM, making it a versatile and widely used programming language.

Simple Java Program Working

The program starts with the declaration of the class HelloWorld using the public access modifier.

Inside the class, we have a main method, which serves as the entry point for the program.

The main method contains a single line of code that uses the System.out.println() method to display the "Hello, World!" message on the console.

Compilation of the Java Program

Once we have written the Java program, we have to compile it using the Java compiler (javac) to generate bytecode. Let's assume our program is saved in a file named We can compile it by executing the following command in the terminal:


This command invokes the Java compiler (javac) and instructs it to compile the file. If the compilation is successful and there are no syntax errors, it generates a bytecode file named HelloWorld.class.

Execution of the Java Program

Now that we have the compiled bytecode, we can execute it using the Java Virtual Machine (JVM). We can run the program using the following command:

java HelloWorld

Here, the Java command launches the JVM and specifies the class name (HelloWorld) to be executed. The JVM loads the bytecode from the HelloWorld.class file and starts executing the main method. As a result, we see the output "Hello, World!" displayed on the console.

Simple Java Program and Its Working:

Here's a simple example of Java source code compiled into bytecode.


Let's delve deeper into how the simple Java program works:

  • The public class SimpleProgram line declares a class named SimpleProgram.

  • Inside the class, the main method is declared as public static void main(String[] args). This is the entry point for the program's execution.

  • The line System.out.println("Hello, world!"); prints the message "Hello, world!" to the console.

  • The next few lines demonstrate a simple calculation. Two variables, a and b, are declared and assigned values 5 and 7, respectively.

  • The line int sum = a + b; calculates the sum of a and b and stores the result in the sum variable.

  • Finally, System.out.println("The sum of " + a + " and " + b + " is " + sum); prints the calculated sum to the console.

This simple Java program showcases basic concepts like variable declaration, assignment, arithmetic operations, and output using System.out.println().

In the Compilation to Execution Process, What Do You Start With?

In the compilation to execution process, the first step is to start with the Java source code. This is typically written in plain text files with a .java extension. It contains the instructions and logic that define the behavior of the Java program.

To compile the Java source code, developers use the Java Development Kit (JDK), which provides the necessary tools for compilation. The primary tool used is the Java compiler, known as "javac." The javac command takes the source code file as input and generates bytecode as output.

Let's take a closer look at the compilation process using our earlier example of the "" program:

  • Save the Java source code in a file named ""

  • Open a command prompt or terminal and navigate to the directory where the source code file is located.

  • Execute the following command to compile the Java source code:  


The javac compiler reads the source code file, performs syntax checking, and generates bytecode if there are no mistakes. If there are any syntax errors, the compiler displays error messages indicating the issues that need to be fixed.

If the compilation is successful, a new file named "HelloWorld.class" is created in the same directory. This contains the bytecode representation of the program.

Now that we have successfully compiled the Java source code, we can proceed to the execution phase.

To execute the compiled Java program, we use the Java Virtual Machine (JVM). It is responsible for loading and interpreting the bytecode generated during compilation.

To run the "HelloWorld" program, execute the following command:

java HelloWorld

The "java" command is used to launch the JVM, and "HelloWorld" specifies the name of the class that contains the main method. The JVM loads the bytecode from the "HelloWorld.class" file and starts executing the program by invoking the main method.

As a result, the program displays the output "Hello, World!" on the console, which was achieved through the System.out.println() statement in the main method.

The Java compilation process involves using the javac compiler to transform Java source code into bytecode. The bytecode is then executed by the JVM, which interprets and runs the program. This combination of compilation and interpretation makes Java a "compiled and interpreted" language.

Is Java Source Code Compiled Into C++

No, Java source code is not directly compiled into C++. Java source code is compiled into Java bytecode, which is a low-level representation of the code that can be executed by the Java Virtual Machine (JVM).

C++ is a separate programming language with its own syntax, features, and compiler. It is not possible to directly compile Java source code into C++ code. The languages have different semantics and libraries, so a direct conversion is not feasible.

If you want to use Java functionality in a C++ program, you would typically need to rewrite or adapt the Java code to C++ syntax manually. This process involves understanding the logic and functionality of the Java code and implementing it using C++ constructs.

However, it's worth mentioning that there are tools and frameworks available that allow for interoperability between Java and C++, such as Java Native Interface (JNI). With JNI, it is possible to create bindings and interface Java code with native C++ code, enabling communication and interaction between the two languages. This approach requires writing additional C++ code to bridge the gap between Java and C++.

Is Java Source Code Compiled Into MCQ?

Java source code is not compiled into Multiple Choice Questions (MCQ).

MCQ refers to a format used for presenting multiple-choice questions and options for answers in quizzes or exams.

On the other hand, Java source code is compiled into bytecode, which is a low-level representation of the code that can be executed by the Java Virtual Machine (JVM). The compilation process in Java involves translating the human-readable Java source code into bytecode using the Java compiler (javac).

So, the Java source code goes through compilation to produce bytecode, not MCQ.


In conclusion, the compilation process in Java is a crucial step that transforms human-readable Java source code into platform-independent bytecode. This bytecode can be interpreted and executed by the Java Virtual Machine. By understanding the compilation and execution process, developers can harness the power of Java to build robust and versatile applications. So whether you are a beginner exploring Java or an experienced developer, learning the compilation process is essential for mastering this powerful programming language.


1. Which of the tools is used to compile Java code?

Ans: The tool used to compile Java code is called the Java compiler. In the Java Development Kit (JDK), the Java compiler is provided as a command-line tool named "javac." It takes the Java source code files as input, typically with a .java extension, and generates bytecode files, typically with a .class extension, as output.

2. How does the Java compiler (javac) convert Java source code into bytecode?

The Java compiler (javac) takes Java source code as input and performs a series of steps to convert it into bytecode. It first performs lexical analysis to break the code into tokens, then parses the tokens to generate an abstract syntax tree (AST). The compiler then performs semantic analysis to check for syntax errors and enforce language rules. Finally, it generates bytecode instructions that represent the compiled code.

3. How can I compile Java source code into bytecode using the command line?

To compile Java source code into bytecode using the command line, you need to have the Java Development Kit (JDK) installed. Open a command prompt or terminal, navigate to the directory containing the Java source file, and use the "javac" command followed by the name of the Java source file. For example, "javac" compiles the "" file and generates the corresponding bytecode file.

4. How does the Java bytecode enable platform independence?

The Java bytecode generated from the compilation process is platform-independent, meaning it can be executed on any system with a compatible Java Virtual Machine (JVM). The JVM interprets the bytecode at runtime and translates it into machine-specific instructions. This abstraction layer provided by the bytecode allows Java programs to run on different platforms without requiring recompilation as long as a JVM is available for that platform.

5. How does the Java Virtual Machine (JVM) interpret and execute the compiled bytecode?

The JVM interprets the compiled bytecode by executing the instructions one by one. It loads the bytecode into memory, performs memory allocation and management, and handles various runtime tasks such as garbage collection. The JVM's interpreter translates the bytecode instructions into machine code specific to the underlying hardware. Additionally, the JVM may also use dynamic optimizations, like Just-In-Time (JIT) compilation, to further enhance the execution performance of the Java program.

Leave a Reply

Your email address will not be published. Required fields are marked *