Syntax & First Program
Now that Java is installed, let's take a closer look at the Hello World program from earlier and break down exactly what every piece means. Understanding this structure now will make everything that follows much easier.
Anatomy of a Java File
Every Java program starts with a class declaration. The file name must exactly match the public class name, including capitalization.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Piece by piece:
public— an access modifier meaning this class is visible from anywhere.class HelloWorld— declares a class namedHelloWorld. This must be saved asHelloWorld.java.public static void main(String[] args)— the method the JVM looks for and calls first when your program starts.static— meansmainbelongs to the class itself, so the JVM can call it without creating an object first.void—maindoesn't return any value.String[] args— an array of command-line arguments passed in when the program is run.
System.out.println(...)— prints text followed by a new line to the console.
Case Sensitivity
Java is case-sensitive. This means HelloWorld, helloworld, and
HELLOWORLD are three completely different identifiers. main must be written exactly in
lowercase — Main or MAIN will not be recognized as the program's entry point.
Semicolons and Braces
Every statement in Java ends with a semicolon ;. Curly braces { } group statements into
blocks — a class body, a method body, or the body of a loop or condition all use braces to mark where the block
starts and ends.
if (isReady) {
System.out.println("Go!");
}
Comments
Comments are ignored by the compiler and exist purely to explain your code to humans. Java supports two styles:
// This is a single-line comment
/*
* This is a multi-line comment.
* It can span as many lines as you need.
*/
System.out.println("Comments don't affect output");
Basic Naming Conventions
Java doesn't force these rules on you, but following them makes your code readable to every other Java developer:
| What | Convention | Example |
|---|---|---|
| Class names | PascalCase — each word capitalized | HelloWorld, BankAccount |
| Method & variable names | camelCase — first word lowercase, rest capitalized | main, totalAmount, printReport |