Java Foundations

Lesson 1 of 7

Hello, Java

Java is a statically-typed, object-oriented language that runs on the JVM (Java Virtual Machine), write once, run anywhere: the same compiled bytecode runs unmodified on any machine with a JVM installed.

Your first program

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

A few things that look unfamiliar if you're coming from Python or JavaScript:

  • Everything lives inside a class. Java has no free-floating functions, main must be a method of some class.
  • public static void main(String[] args) is the fixed, required signature the JVM looks for as the entry point, public (callable from outside), static (belongs to the class itself, not an instance), void (returns nothing), String[] args (command-line arguments).
  • The filename must match the public class name exactly: Hello.java.

Compiling and running

Java compiles to an intermediate form called bytecode, not directly to native machine code:

javac Hello.java   # compiles Hello.java into Hello.class (bytecode)
java Hello          # the JVM runs the bytecode

This two-step process, and the JVM in between, is exactly what makes "write once, run anywhere" true, the same .class file runs on Windows, macOS, or Linux without recompiling, as long as a JVM is installed.

TIP

System.out.println is verbose on purpose, it's read as "the out stream on the System class, call println", once you know Java always nests functionality inside classes, the verbosity stops feeling arbitrary.

📝 Hello, Java Quiz

Passing score: 70%
  1. 1.What does the JVM actually execute?

  2. 2.In Java, a function can exist on its own, outside of any class.

  3. 3.The command that compiles Hello.java into bytecode is: ____ Hello.java