Skip to main content

From javac to JVM

Understanding the Java Execution Process

Updated 7 min readMarkdown
From javac to JVM

When we write a Java program, we write code that humans can understand. But a computer's CPU ultimately works with low-level machine instructions represented as bits (0s and 1s).

So, what happens between our Java code and those 0s and 1s?

1. We Write Java Source Code

First, we write a Java program in a .java file.

For example:

class Hello {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

This is called source code.

It is written in a high-level programming language, Java, which is designed to be relatively easy for humans to read and write.

2. The Java Compiler Compiles the Program

We use the Java compiler called javac.

For example:

javac Hello.java

The compiler checks the Java program and converts the source code into bytecode.

The result is usually:

Hello.class

So:

Hello.java
      ↓
    javac
      ↓
Hello.class

3. What Is Bytecode?

The .class file contains JVM bytecode.

Bytecode is an intermediate form of instructions designed for the Java Virtual Machine (JVM).

It is not the same thing as the final machine code executed directly by a particular CPU.

For example, bytecode contains instructions such as:

aload_0
invokevirtual
return

These are instructions understood by the JVM.

Therefore:

Java source code → Bytecode

not:

Java source code → CPU machine code directly

4. Then Where Are the 0s and 1s?

This was one of my main doubts when learning Java.

If computers ultimately work with 0s and 1s, where are they?

The answer is that there are different levels of representation.

Java Source Code
       ↓
Java Bytecode
       ↓
JVM
       ↓
Machine Instructions
       ↓
CPU
       ↓
Hardware signals / bits

The CPU ultimately operates on machine-level instructions represented internally as binary data.

So the 0s and 1s are not something we normally see while programming Java.

Java hides these low-level details from the programmer.

5. What Does the JVM Do?

The JVM (Java Virtual Machine) is the environment that runs Java bytecode.

When we execute:

java Hello

the JVM loads the .class file and executes its bytecode.

The JVM can use different techniques to execute the bytecode, including interpretation and Just-In-Time (JIT) compilation.

The JIT compiler can compile frequently executed bytecode into native machine code for the particular CPU.

So the process can be represented as:

Java Code
   ↓
javac
   ↓
Bytecode
   ↓
JVM
   ↓
JIT Compiler
   ↓
Native Machine Code
   ↓
CPU

6. Why Don't We See the Machine Code?

When we run:

java Hello

we see:

Hello

We don't see the machine instructions or binary because the JVM and operating system handle those details for us.

Our program requests an operation such as:

System.out.println("Hello");

and the Java runtime, JVM, operating system, and hardware work together to produce the visible output.

7. How Do We Actually Get the Output?

Let's follow this statement:

System.out.println("Hello");

At a simplified level:

Java statement
     ↓
Compiled into bytecode
     ↓
JVM executes the bytecode
     ↓
Java runtime performs the required operations
     ↓
Operating system / hardware handle output
     ↓
"Hello" appears on the screen

The exact internal path is more complicated than this, but this model is useful for understanding the basic idea.

8. Is Bytecode Machine Code?

No.

This is an important distinction.

Code Meaning
Java source code Human-readable Java program
Bytecode Intermediate instructions for the JVM
Machine code Instructions for a particular CPU architecture
Binary Representation using bits such as 0 and 1

Therefore:

Bytecode is not the same as CPU machine code.

9. Then Why Is Java Platform Independent?

This is one of the most interesting features of Java.

Suppose we compile:

Hello.java

into:

Hello.class

The bytecode is designed for the JVM rather than a specific CPU.

Therefore, the same Java bytecode can run on different operating systems if a compatible JVM is available.

For example:

             Hello.java
                 ↓
               javac
                 ↓
            Hello.class
             Bytecode
                 ↓
       ┌─────────┼─────────┐
       ↓         ↓         ↓
 Windows_JVM  Linux_JVM  macOS_JVM
       ↓         ↓         ↓
      CPU       CPU       CPU

This is the basic idea behind:

Write Once, Run Anywhere.

10. What Is javac?

javac is the Java compiler.

It converts Java source code into JVM bytecode.

javac Hello.java

Result:

Hello.class

Remember:

javac → compile
java  → run

11. What Is java?

When we write:

java Hello

we are asking the Java runtime to start the Hello class.

The JVM loads and executes the bytecode.

So:

javac Hello.java

means:

Compile the Java source code.

While:

java Hello

means:

Run the compiled Java class.

12. Important Beginner Doubts

If computers understand 0 and 1, why do we write Java?

Because programming directly in machine code would be extremely difficult.

Java provides a high-level language that humans can work with, while the compiler and JVM handle much of the low-level complexity.

Is bytecode made of 0s and 1s?

At the file/storage level, everything is ultimately represented as binary data.

However, when we talk about Java bytecode, we mean the JVM's instruction set and class-file format, not simply "a bunch of 0s and 1s."

So saying:

"Bytecode is machine language"

is misleading.

A better statement is:

Bytecode is an intermediate instruction format designed for the JVM.

If bytecode is not machine code,howdoes the CPU execute Java?

The CPU does not directly execute JVM bytecode as if it were native CPU instructions.

The JVM executes the bytecode and may interpret it or use JIT compilation to produce native machine code that the CPU can execute.

Why don't I see 0s and 1s when running Java?

Because the Java development tools, JVM, operating system, and hardware hide those low-level details.

You see the program's output rather than the instructions used internally to produce it.

Does javac directly create machine code?

Normally, javac creates JVM bytecode, not CPU-specific native machine code.

The JVM can later use JIT compilation to produce native machine code during execution.

13. The Complete Picture

The complete simplified process is:

       Human
         ↓
Java Source Code
     Hello.java
         ↓
       javac
         ↓
     Bytecode
     Hello.class
         ↓
        JVM
         ↓
 Interpretation / JIT Compilation
         ↓
 Native Machine Instructions
         ↓
        CPU
         ↓
      Hardware
         ↓
       Output

This explains the journey from the code we write to the instructions ultimately executed by the computer.

Conclusion

Java does not normally translate our source code directly into CPU machine code using javac. Instead, javac produces JVM bytecode, which is then executed by the JVM. The JVM can interpret the bytecode and use JIT compilation to produce native machine instructions for the CPU.

So the next time we write:

System.out.println("Hello");

there is a lot happening behind that simple line from Java source code to bytecode, JVM execution, native machine instructions, and finally the output we see on the screen.

I hope that you’ve found this blog helpful...! If you have any questions or feedback, feel free to leave a comment below.

If you found this article helpful, please like and share it .

No comments yet. Be the first to comment.