Introduction to Java

Java has been running banking systems, Android apps, and half the enterprise software on the planet for three decades. It's verbose compared to newer languages, but that verbosity buys you something: code that tells you exactly what it's doing, and a compiler that catches your mistakes before anyone else sees them.

Write once, run anywhere

Most languages you write get compiled straight into instructions your specific processor understands. Java takes a detour. When you compile a .java file, you don't get machine code — you get bytecode, a kind of universal intermediate format stored in a .class file. That bytecode runs inside the Java Virtual Machine, or JVM, which translates it into real instructions for whatever computer it happens to be sitting on.

This is why the same compiled Java program runs unmodified on Windows, macOS, and Linux, as long as each machine has a JVM installed. Sun Microsystems marketed this idea in the 90s as "write once, run anywhere," and while every language has caught up in various ways since, it's still the reason Java became the default choice for large, long-lived systems that needed to run on machines nobody could fully predict in advance.

It also explains why Java is statically typed and compiled ahead of time rather than interpreted line-by-line. Because the compiler checks your entire program's types and structure before it ever runs, whole categories of bugs — passing a string where a number was expected, calling a method that doesn't exist — get caught at compile time instead of surfacing as a crash in production at 2am.

Getting a JDK

To write and run Java, you need a JDK — a Java Development Kit. This bundles the compiler (javac), the runtime that executes your programs (java), and the standard library. You do not need just a JRE (Java Runtime Environment); a JRE can only run already-compiled Java, not compile your source code.

You can download a JDK from Oracle, or use a free open-source build like Eclipse Temurin or Amazon Corretto — any of these will work fine for learning. Once it's installed, typing java -version in a terminal should print a version number back at you. That's your confirmation everything is wired up correctly.

Your first program

Unlike a script in Python or JavaScript, you can't just write loose lines of Java in a file and run them. Every bit of executable Java code has to live inside a class, because Java is an object-oriented language down to its bones — even a program that does nothing but print a greeting still needs a home for that instruction. Save the following as Hello.java:

</> Hello.java
// A minimal Java program
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

Two things about this file matter more than the rest. First, the file name has to match the public class name exactly — Hello.java for class Hello — or the compiler will refuse to build it. Second, main is special: it's the method the JVM looks for and calls automatically when you run the program. Nothing happens unless it's inside main (or called from something that eventually runs inside it).

To turn this into a running program, you compile it and then run the result as two separate steps:

</> terminal
javac Hello.java
java Hello
Output
Hello, Java!

javac is the compiler — it reads Hello.java and produces Hello.class, the bytecode file. java is the launcher — notice it takes the class name, Hello, not the filename. It hands that bytecode to the JVM, which runs it.

Breaking down the ceremony

  • public class Hello — declares a class named Hello that other code is allowed to see and use.
  • public static void main(String[] args) — the entry point. static means this method belongs to the class itself, not to any particular object of it; void means it doesn't hand back a value; String[] args catches any command-line arguments passed in when the program starts.
  • System.out.println(...) — writes text to the console, followed by a line break. System.out is a built-in output stream that's always available; println is one of the methods it offers.

If you want to print without adding a new line afterward, there's a sibling method for that too:

</> PrintDemo.java
public class PrintDemo {
    public static void main(String[] args) {
        System.out.print("Loading");
        System.out.print("...");
        System.out.println();
        System.out.println("Done.");
    }
}
Output
Loading...
Done.
Note: Java is case-sensitive and picky about matching names — Hello.java must contain class Hello, not class hello or class HelloWorld. Get used to reading compiler errors closely; they're blunt, but they're usually telling you exactly what's wrong.