</>
Skip to content
Java lessons (4/47)

Java — Syntax

Statements end with semicolons

Every complete instruction in Java ends with a semicolon:

int age = 25;
String name = "Ada";
System.out.println(name);

Forget the semicolon and the compiler stops you immediately. This is the single most common beginner mistake in Java — and the easiest to fix.

Blocks are wrapped in curly braces

A block groups multiple statements together. Java uses blocks for almost everything — methods, classes, loops, conditions:

public class Main {
    public static void main(String[] args) {
        System.out.println("Line one");
        System.out.println("Line two");
    }
}

Every opening { must have a matching closing }. IDEs auto-complete them; counting braces manually is error-prone.

Indentation is not required

Unlike Python, Java doesn't care about whitespace. The following compiles fine:

public class Main{public static void main(String[] args){System.out.println("ugly");}}

But please don't. Every Java style guide insists on consistent indentation (4 spaces is standard) and placing each statement on its own line. Readable code is maintainable code.

Case sensitivity

Java is case-sensitive. These are four different identifiers:

int age = 25;
int Age = 30;
int AGE = 35;
int aGe = 40;

All four compile. All four hold different values. Class names start uppercase (Main, String), variables and methods start lowercase (age, getName). Follow this convention or other Java developers will struggle to read your code.

Reserved words

Java reserves a set of keywords that you cannot use as variable names:

class      public     static     void       int
String     if         else       for        while
return     break      continue   new        this
true       false      null       try        catch

Trying to name a variable class or int gives a compile error. Your IDE highlights these in a distinct color — learn to recognize them.

Comments

Java supports three comment styles:

// Single-line comment

/*
   Multi-line comment
   spans several lines
*/

/**
 * Documentation comment.
 * Used by tools like Javadoc to generate reference docs.
 */

Single-line comments are for quick notes. Multi-line comments explain complex blocks. Documentation comments describe public APIs — they're not just notes, they're a contract with other developers.

Import statements

Java's standard library is vast. You access classes from other packages with import:

import java.util.Scanner;
import java.util.ArrayList;

When you use Scanner, Java needs the import to know exactly which Scanner class you mean. Without it, you'd have to type the full path every time:

java.util.Scanner sc = new java.util.Scanner(System.in);

Importing once at the top saves repetition and keeps code clean.

Naming conventions

Java has strong community conventions. Following them makes your code feel native:

ElementConventionExample
ClassPascalCaseStudentRecord
MethodcamelCasegetStudentName
VariablecamelCasestudentCount
ConstantUPPER_SNAKE_CASEMAX_RETRY_COUNT
Packagelowercasecom.example.app

These aren't enforced by the compiler — they're enforced by culture. Every Java codebase you'll encounter follows them.

Whitespace and readability

Java ignores extra spaces and blank lines:

int   x   =   5;

is the same as:

int x = 5;

But whitespace shapes readability. One space around operators, blank lines between logical sections, consistent indentation — these habits make code scan-friendly.

Line length

Most style guides cap lines at 80–120 characters. When a line gets too long, break it naturally:

String fullName = firstName + " " + middleName + " " + lastName;

becomes:

String fullName = firstName + " "
    + middleName + " "
    + lastName;

The compiler doesn't care about line breaks. Your teammates do.

Mini Practice

  1. Write a program with three println statements — each printing a different line of a poem
  2. Add a multi-line comment above the statements explaining what the program does
  3. Create two variables: userName (String) and userAge (int) — print both
  4. Try naming a variable class — read the error
  5. Break a long string across two lines using + concatenation

Next: how Java prints output to the console →

Related Topics

Frequently Asked Questions about Syntax

What is Syntax in Java?

Syntax is a fundamental concept in Java. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Syntax?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Syntax.

Why is Syntax important in Java?

Syntax is essential for Java development. Understanding this concept will help you write better code and solve real-world problems more effectively.