Turning Learners Into Developers
Codekilla
CODEKILLA
back to course
Lesson 03 / 804%· free preview
Getting Started3/4

Java Syntax

Classes, methods, statements, blocks, comments, and the rules that make Java predictable.

Java Program Skeleton

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!");
    }
}
Statements & Semicolons

A statement is one instruction. Every Java statement ends with a semicolon ;.

java
int x = 10;                          // statement 1
System.out.println(x);               // statement 2

Forgetting ; is the #1 beginner error: ';' expected.

Blocks (Compound Statements)

Code surrounded by { } is a block. Classes, methods, loops, ifs — all use blocks.

java
if (x > 0) {
    System.out.println("positive");
    System.out.println("number");
}
Comments

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
 */
Case Sensitivity

Java is case-sensitive:

java
int Age = 21;
System.out.println(age);  // ERROR — 'age' is not declared (it's 'Age')
Naming Rules (Identifiers)
  • 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.
Reserved Keywords (selection)

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.

Output: `System.out`
java
System.out.print("Hello ");          // no newline
System.out.println("Codekilla!");    // adds newline
System.out.printf("%s is %d%n", "Java", 30);  // C-style printf
Input: `Scanner`
java
import java.util.Scanner;

Scanner sc = new Scanner(System.in);
System.out.print("Age? ");
int age = sc.nextInt();
System.out.println("Hello, " + age + "-year-old!");
A Complete Example
java
import 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!
Common Mistakes
  • Missing semicolonint x = 10 without ; triggers ';' expected.
  • File name mismatchpublic class Greet must live in Greet.java.
  • Calling System.out.println outside a method — must be inside main or another method.
  • Forgetting import — using Scanner without import java.util.Scanner;cannot find symbol.
  • Mismatched braces — every { needs a }. The compiler reports far from the actual mistake.
Interview Questions

Practice Exercises
  1. Sum two numbers — Read two ints with Scanner and print their sum. Hint: sc.nextInt().
  2. Fix the bugsSystem.out.println("Hello") (missing ;) and Scanner sc = Scanner(System.in); (missing new). Hint: read the compiler messages.
  3. Echo your name — Read a String and print Hello, NAME!. Hint: nextLine() reads a full line including spaces.
  4. 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.

AI-powered recap

Quick recap quiz?

We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.

# program

Program

Java
public class Main {
    public static void main(String[] args) {
        System.out.printf("Hello %s, you are %d years old%n", "Codekilla", 5);
    }
}
Ready to move on?
// example library
Want more hands-on snippets in Java?
Browse 100 runnable examples · across 10 chapters · short, copy-paste-friendly · grouped by topic
Explore examples
// glossary lookup
Every term you just saw, explained
Quick definitions for variables, pointers, loops, functions and every concept in one searchable page.
Open Terminology
// feedback.matters()
Did this lesson help you?