Welcome to Java
You know Python basics, so variables, loops, and functions are already familiar. Java expresses the same ideas with more structure, and that structure is why it runs so much of the serious world: Android apps, bank and airline backends, and most large company codebases. Teams of hundreds can work on one Java program because the compiler checks everyone's code against everyone else's before anything runs.
Two big differences up front:
- Everything lives inside a class. A class is a named container for code. Even a one-line script needs one.
- Java is compiled and statically typed. You declare the type of every variable, and a compiler checks your whole program before it runs.
In Python you print with print("hi"). In Java the same idea takes a few more lines, and every one of them has a job. The smallest complete Java program is next.
The smallest complete program
Four lines of scaffolding around one line of work. Read each line and note what it contributes.
public class Main { public static void main(String[] args) { System.out.println("Hello, Java!"); } }
Output
Hello, Java!
Only the third line produces output. The rest exists to tell Java where the program lives and where it starts, and the next section takes it apart piece by piece.
Anatomy of the program
public class Main { ... }declares a class namedMain. The file is named after it, soMain.java, and all your code sits inside its braces.public static void main(String[] args)is the main method. Java starts every program by searching for a method with exactly this signature, so changing one word stops the program from launching. Each keyword has a precise meaning, decoded in unit 4. Until then, copy the line exactly.System.out.println("Hello, Java!");prints a line of text. It is Java's equivalent ofprint().- Every statement ends with a semicolon. Python uses line breaks to end a statement, Java uses that punctuation mark.
- Braces group code the way indentation does in Python. Java ignores indentation entirely, but you still indent for human readers.
| Java | Python equivalent |
|---|---|
public class Main { } | no equivalent, code sits at top level |
public static void main(...) | the script body itself |
System.out.println(x) | print(x) |
{ } | indentation |
; | end of line |
Misspell main or leave off a statement terminator and the program will not even start. The compiler reports the mistake first.
The file name follows the class name
A file declaring public class Main must be named Main.java, matching the class name exactly, capitalization included.
Java requires a public class to live in a file named after it. The rule exists so that the compiler and other programmers can find any class from its name alone, and in a project with thousands of classes nobody has to guess which file holds Dog.
Main.class is a different thing. That is the compiler's output, the bytecode file, and it is generated rather than written by hand.
The rule catches beginners often, because the error message talks about the class rather than the file name. Renaming the class and forgetting to rename the file produces a compile error before a single line runs.
Why a bare statement will not compile
In Python, print("hi") at the top of a file runs on its own. Java rejects it, because all Java code must live inside a class, and execution starts at the main method.
The nesting is strict. Every statement lives inside a method, and every method lives inside a class. When you run a program, Java locates the class's main method and begins there, so a loose top-level statement has no place to belong and produces a compile error.
System.out.println("hi"); // error: not inside a class or method
The verbosity buys something real. Because every piece of code has a named home, a tool can tell you exactly which class and method any line belongs to, which is what makes navigating a million-line codebase possible.
Printing two lines
Each System.out.println(...) prints its text and then moves to a new line, so two lines of output need two statements.
public class Main { public static void main(String[] args) { System.out.println("Ada Lovelace"); System.out.println("learning Java"); } }
Output
Ada Lovelace learning Java
Both statements sit inside main and each one ends with a semicolon. Statements run top to bottom in the order they are written, so swapping the two lines swaps the output.
The indentation is for readers only. Java would accept both statements on one line, and the compiler would not notice the difference.