Java Syntax
Classes, methods, statements, blocks, comments, and the rules that make Java predictable.
Every Java program follows this skeleton:
java// 1. Optional package declaration package com.codekilla; // 2. Optional imports import java.util.Scanner; // 3. Class declaration — file MUST be named ClassName.java public class Demo { // 4. Main method — entry point public static void main(String[] args) { // 5. Statements go here System.out.println("Hi!"); } }
A statement is one instruction. Every Java statement ends with a semicolon ;.
javaint x = 10; // statement 1 System.out.println(x); // statement 2
Forgetting ; is the #1 beginner error: ';' expected.
Code surrounded by { } is a block. Classes, methods, loops, ifs — all use blocks.
javaif (x > 0) { System.out.println("positive"); System.out.println("number"); }
Three flavours:
java// Single-line comment /* Multi-line comment */ /** Javadoc — used by IDEs and the `javadoc` tool to generate HTML docs. * @param args command-line arguments */
Java is case-sensitive:
javaint Age = 21; System.out.println(age); // ERROR — 'age' is not declared (it's 'Age')
- Start with a letter,
$, or_:score,_total,$value,userName. - Can contain letters, digits,
_,$:score1,total_amount. - Cannot start with a digit: ❌
1score. - Cannot be a keyword: ❌
int, ❌class, ❌return.
abstract, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, enum, extends, final, finally, float, for, if, implements, import, instanceof, int, interface, long, new, null, package, private, protected, public, return, short, static, super, switch, this, throw, throws, try, void, volatile, while.
javaSystem.out.print("Hello "); // no newline System.out.println("Codekilla!"); // adds newline System.out.printf("%s is %d%n", "Java", 30); // C-style printf
javaimport java.util.Scanner; Scanner sc = new Scanner(System.in); System.out.print("Age? "); int age = sc.nextInt(); System.out.println("Hello, " + age + "-year-old!");
javaimport java.util.Scanner; public class Greet { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Your name: "); String name = sc.nextLine(); System.out.print("Your age: "); int age = sc.nextInt(); System.out.printf("Hello %s, you are %d!%n", name, age); } }
Output (input Aarav, 21):
Your name: Aarav
Your age: 21
Hello Aarav, you are 21!
- Missing semicolon —
int x = 10without;triggers';' expected. - File name mismatch —
public class Greetmust live inGreet.java. - Calling
System.out.printlnoutside a method — must be insidemainor another method. - Forgetting
import— usingScannerwithoutimport java.util.Scanner;→cannot find symbol. - Mismatched braces — every
{needs a}. The compiler reports far from the actual mistake.
- Sum two numbers — Read two ints with
Scannerand print their sum. Hint:sc.nextInt(). - Fix the bugs —
System.out.println("Hello")(missing;) andScanner sc = Scanner(System.in);(missingnew). Hint: read the compiler messages. - Echo your name — Read a
Stringand printHello, NAME!. Hint:nextLine()reads a full line including spaces. - Comment cleanup — Take any 10-line Java snippet and add
//comments explaining each line. Hint: comments make code readable for the next developer.
💡 Think Like a Programmer: Syntax is the grammar of code. Get it wrong and the compiler refuses to talk to you. Get it right and the compiler becomes your friendliest teacher — its error messages will guide every fix.
Quick recap quiz?
We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.
Program
public class Main {
public static void main(String[] args) {
System.out.printf("Hello %s, you are %d years old%n", "Codekilla", 5);
}
}