☕ Java Foundations · Beginner

What Java is

A compiled, statically typed, object-oriented language that runs on the JVM: write once, run anywhere.

🧩 The mysteryThe same Java file runs on Windows, macOS and Linux without changing a single byte. How can one file speak every computer's language?

Computers speak different languages

A CPU only understands its own machine code. A program built for one kind of chip or operating system usually won't run on another. Java's trick: don't compile to machine code at all. Compile to an in-between language called bytecode.

Bytecode and the JVM

You write source code in a .java file. The compiler javac turns it into bytecode in a .class file. Then the JVM (Java Virtual Machine), a program installed on each computer, runs that bytecode. Think of bytecode as sheet music, and every JVM as an orchestra that can play it.

Hello.java   (you write this)
    | javac
Hello.class  (bytecode)
    | JVM on any OS
program runs
🤔 Think first

Think about it

You compiled your app on a Mac. You copy the .class files to a Linux server with the same Java version. What must you do before running them there?

Think about it, then reveal the answer

Nothing. The Linux JVM reads the very same bytecode. No recompiling, no converting. That's Java's famous promise: write once, run anywhere.

Statically typed

Every variable has a type that is fixed when you write the code, and the compiler checks it before the program runs. A variable declared int can only ever hold whole numbers.

int count = 5;     // count holds ints
count = 10;        // fine: still an int
🔮 Predict it

Your turn

What happens when you try to run this?

int count = 5;
count = "five";
System.out.println(count);
  1. five
  2. 5
  3. Compile error
Show the answer

Compile error. count is an int, so a String can't go in. javac rejects the program and it never even starts. Catching mistakes this early is what static typing is for.

Built from objects

Java is object-oriented: programs are built from classes (blueprints) and objects (things made from them). You'll build your own soon. For now, just know that almost everything in Java lives inside a class.

⚠️ The trap

Don't look for free()

Coming from C? There you free memory yourself with free(), and forgetting causes leaks and crashes. Java has no free(). The JVM's garbage collector automatically reclaims objects your program can no longer reach.

💼 In the real world

In real projects

Java runs bank backends, huge web services and Minecraft: Java Edition. Teams compile once and ship the same bytecode to developer laptops, test servers and cloud machines, whatever operating system they run.

Key takeaways

  1. Compiled to bytecode, not to one CPU's machine code
  2. Statically typed: types are checked at compile time
  3. Object-oriented: programs are built from classes and objects
  4. Write once, run anywhere: any OS with a JVM runs the same bytecode

💡 Bytecode is like sheet music: any orchestra (JVM) on any stage (OS) can play it.

🤯 Did you know?

Java was first called Oak, after a tree outside creator James Gosling's office. The name was changed to Java, after the coffee, before the language's public release in 1995.

Practice questions

What does "write once, run anywhere" mean for Java?

  1. Each operating system interprets the .java source line by line
  2. The same compiled .class files run on any OS that has a compatible JVM
  3. javac outputs a universal machine code that every CPU understands
  4. Java programs always run inside a web browser
Check your answer

The same compiled .class files run on any OS that has a compatible JVM. The portable part is the bytecode. Each platform supplies its own JVM, so the same compiled program runs on Windows, macOS or Linux without recompiling.

What does this print?

int count = 5;
count = "five";
System.out.println(count);
  1. five
  2. 5
  3. Throws ClassCastException
  4. Compile error
Check your answer

Compile error. count is declared as an int, so assigning a String is a type mismatch. Java is statically typed, so javac rejects the program before it can run.

Next: JDK, JRE, JVM. Three acronyms everyone mixes up. Which one actually runs your code?