Language Fundamentals
Anatomy of a Java Program
public static void main(String[] args); javac compiles source to bytecode that the JVM executes anywhere.- One public top-level class per
.javafile, named exactly after the file mainmust bepublic static voidand takes aString[]of command-line argumentsjavacproduces.classbytecode;javaruns 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.
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.
$ javac HelloWorld.java // produces HelloWorld.class
$ java HelloWorld // runs the bytecode on the JVM
$ java HelloWorld.java // Java 11+: compile-and-run in one stepComments 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.