Language Fundamentals

Anatomy of a Java Program

A Java program is a set of classes. Execution starts at public static void main(String[] args); javac compiles source to bytecode that the JVM executes anywhere.
  • One public top-level class per .java file, named exactly after the file
  • main must be public static void and takes a String[] of command-line arguments
  • javac produces .class bytecode; java runs it on the JVM — compile once, run anywhere
  • Since Java 11 you can run a single source file directly: java Hello.java
  • JShell provides a REPL for interactive experimentation

Java is case-sensitive, every statement lives inside a class, and every executable program needs an entry point: a main method with the exact signature below. The JVM calls it with the command-line arguments — note that unlike C, args[0] is the first argument, not the program name.

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

The compiler turns source into bytecode — instructions for the Java Virtual Machine, not for any physical CPU. This is the root of Java's portability: the same .class file runs on any platform with a JVM. How the JVM executes and optimizes that bytecode is covered in Jvm Architecture and Jit Compilation.

Compile and run
$ javac HelloWorld.java   // produces HelloWorld.class
$ java HelloWorld         // runs the bytecode on the JVM
$ java HelloWorld.java    // Java 11+: compile-and-run in one step

Comments and documentation

Java has three comment forms: // line, /* block */, and /** documentation */. Javadoc comments placed before public classes and members are extracted by the javadoc tool into browsable API documentation — the same system that produces the official JDK docs.

Sources
  • Core Java, Volume I: Fundamentals (13th ed.)Ch. 2–3 — Programming Environment; Fundamental Structures
  • Learning Java (6th ed.)Ch. 2–3 — A First Application; Tools of the Trade
  • Effective Java (3rd ed.)Items 25, 68