A compiler is a program that translates source code written in a high-level language (like Java, C, or Kotlin) into another form — usually:

  • Machine code (for direct execution by the CPU)
    or

  • Intermediate code (like Java bytecode, which runs on a VM)


🔸 What a Compiler Actually Does

It analyzes your code and performs multiple stages:

StageWhat It Does
Lexical AnalysisBreaks code into tokens (keywords, variables, etc.)
Syntax AnalysisChecks grammar (e.g., matching {}, method structure)
Semantic AnalysisValidates type safety, variable usage, etc.
Intermediate Code GenerationConverts code to bytecode or IR
OptimizationImproves performance (e.g., removes dead code)
Code GenerationOutputs bytecode or native machine code

🔹 Java Compiler (javac)

For Java, the compiler:

  • Takes .java files

  • Produces .class files containing bytecode

  • That bytecode runs on the JVM, not directly on the CPU

javac MyApp.java   # Compile to bytecode
java MyApp         # JVM runs it

🔸 Compiler vs Interpreter

FeatureCompilerInterpreter
OutputMachine code or bytecodeExecutes code line by line
SpeedFaster after compilationSlower due to real-time parsing
Java Usesjavac for compilation → JVM runs/interpretsJVM includes both interpreter & JIT compiler

✅ TL;DR

A compiler converts source code into lower-level code (machine code or bytecode), so it can be executed efficiently by hardware or a virtual machine.

Let me know if you want to walk through how javac translates a specific Java snippet into bytecode.