Sitemap
Shalini Venkatesan

Shalini Venkatesan

Final year Computer Science student | Learning full-stack dev, DSA & core CS | Writing weekly reflections

How Java Code Becomes Machine Code: A Beginner’s Guide

3 min readSep 25, 2025

--

We know that computers can only understand 0s and 1s — also called machine code.

But here’s the thing: we don’t write instructions in binary, do we? That would be a tedious process and nearly impossible to write a full application in raw 0s and 1s.

That’s exactly why programming languages exist. They’re a way for us to give instructions to a computer in a form we can actually read and write.

Why Do We Need Conversion?

As I said before, computers only understand machine code, not the code we humans write. So, there must be a way to convert source code into machine code.

In general, this can be done in two ways: compilation or interpretation.

Compilation

Compilation is the process of converting the entire source code into a separate executable file, which can then be run.

Examples: C, C++.

👉 Think of it like reading a full paragraph written in English and then translating the entire thing into Tamil. After that, anyone can just read the Tamil version without needing to know English.

Interpretation

Interpretation, on the other hand, translates line by line into machine code while executing at the same time.

Examples: Python, JavaScript.

👉 It’s like having a translator in a conversation. The translator doesn’t wait for the speaker to finish the entire speech; instead, they translate sentence by sentence in real time.

But What About Java?

Java is a little different. It doesn’t purely compile like C, nor does it purely interpret line by line. Instead, it uses a hybrid approach.

Why? Because both compilation and interpretation have drawbacks:

  • A compiled file is platform dependent. An executable compiled on Windows won’t run on Linux or Mac, because each operating system and CPU architecture has its own way of understanding machine code.
  • Pure interpretation can be slower, since it translates line by line at runtime.

Java solves this with the concept of bytecode.

The Idea of Bytecode

Instead of compiling source code directly into machine code, Java first compiles it into an intermediate form called bytecode.

  • Bytecode is stored in .class files.
  • The Java compiler (javac) is responsible for converting your .java source file into this bytecode.

So far so good. But wait… how can this bytecode run on every operating system?

Enter the JVM

The magic lies in the Java Virtual Machine (JVM).

  • The JVM is installed on every operating system.
  • Each JVM is designed specifically for its platform (Windows, Linux, Mac, etc.).
  • The JVM knows how to convert bytecode into native machine code for that OS.

So, the bytecode itself is platform independent, but the JVM is platform specific.

Get Shalini Venkatesan’s stories in your inbox

Join Medium for free to get updates from this writer.

This is why Java is famously known for “Write once, run anywhere.”

Why Not Compile Directly to Machine Code?

You might wonder: if the JVM is converting to machine code anyway, why not just compile the source directly into machine code?

Here’s why:

  • Compilation to machine code is heavy and time-consuming.
  • Bytecode is compact and only needs to be compiled once.
  • The JVM uses techniques like Just-In-Time (JIT) compilation to efficiently convert bytecode into machine code at runtime.

This balance makes Java both efficient and portable.

The Big Picture: From Your Keyboard to the CPU

Let’s connect the dots with a concrete example. When you write a simple HelloWorld.java program, here's the journey it takes:

  • Source Code (HelloWorld.java)
    -
    The human-readable Java code you write.
    - Example command to compile:
javac HelloWorld.java
  • Bytecode (HelloWorld.class)
    -
    Platform-independent intermediate code generated by the compiler.
    - Stored in a .class file.
    - Run the program with:
java HelloWorld
  • Machine Code (0s and 1s)
    -
    The JVM loads the .class file and translates the bytecode into native machine instructions specific to your OS and CPU.
    - This is what actually executes and produces the output:
Hello, World!

✨ Next time you run a simple Java HelloWorld, remember the whole journey happening behind the scenes!

Teaser for Next Post

The JVM doesn’t just execute bytecode — it also manages memory, garbage collection, and optimizes performance.
And what about the JDK and JRE you keep hearing about? How do they fit into the picture?
That’s exactly what I cover in my next blog post: JDK vs JRE vs JVM — What’s the Difference?

--

--

Shalini Venkatesan
Shalini Venkatesan

Final year Computer Science student | Learning full-stack dev, DSA & core CS | Writing weekly reflections