Java Syntax
Java is precise. Every program has the same skeleton.
The minimum Java program
Every executable Java file has at least:
- One class declaration (the file
Hello.javacontainsclass Hello) - One main method — the entry point
- Curly braces
{ }defining code blocks - Semicolons
;ending each statement
public class Hello { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Breaking it down
public class Hello — declares a class named Hello. The keyword public means anyone can use this class. The filename must match: Hello.java.
public static void main(String[] args) — the main method, where execution begins:
public— accessible from anywherestatic— belongs to the class, not an instance (you don't need to create aHelloobject to call it)void— returns nothingString[] args— receives command-line arguments as an array of strings
System.out.println(...) — prints a line to the console.
Statements and blocks
A statement is one instruction. Java statements end with a semicolon:
int age = 28; String name = "Raman"; System.out.println(name);
A block groups statements with curly braces. Methods, loops, classes — all use blocks.
Comments
Java has three comment styles:
// Single-line comment /* Multi-line comment block */ /** * Documentation comment (Javadoc). * @param args command-line arguments */
Case sensitivity
Java is strictly case-sensitive. Name, name, and NAME are three different identifiers. main works; Main doesn't.
Naming conventions
- Classes:
PascalCase—Customer,OrderProcessor - Methods, variables:
camelCase—getName,totalPrice - Constants:
UPPER_SNAKE_CASE—MAX_RETRIES,PI - Packages:
lowercase—com.codewithraman.utils