Java Syntax, Keywords, Identifiers & Comments
Learn Java's case-sensitive syntax rules, reserved keywords, valid identifier rules, Unicode system, and single/multi-line/Javadoc comments.
- Write syntactically valid Java classes, methods, and blocks.
- Apply legal identifier rules and avoid Java's reserved keywords.
- Document code cleanly with single-line, multi-line, and Javadoc comments.
- Understand how Java's 16-bit Unicode system supports international characters.
Introduction
Java syntax is strict, predictable, and case-sensitive. Every statement ends with a semicolon (;), code blocks are enclosed in curly braces ({ }), and every executable instruction lives inside a class or interface. Understanding these lexical rules early makes compiler error messages immediately clear.
Learning Objectives
- Write syntactically valid Java classes, methods, and blocks.
- Apply legal identifier rules and avoid Java's reserved keywords.
- Document code cleanly with single-line, multi-line, and Javadoc comments.
- Understand how Java's 16-bit Unicode system supports international characters.
Lexical Structure & Statement Rules
A Java source file is composed of tokens: keywords, identifiers, literals, operators, and separators. Whitespace (spaces, tabs, newlines) does not affect execution—unlike Python, Java uses { and } rather than indentation to define block scope. However, consistent 4-space indentation is universally expected in Java codebases.
Rules for Java Identifiers
An identifier is the name of a class, variable, method, or package:
- May contain letters (
A–Z,a–z), digits (0–9), underscore (_), and dollar sign ($). - Must not start with a digit (e.g.,
2ndUseris invalid;user2is valid). - Case-sensitive (
studentCountandStudentCountare distinct). - Cannot be a reserved keyword (
class,int,public,static,return,final,void,if,while, etc.). - Cannot contain spaces or special symbols like
@,#,-.
Comments in Java
- 1Single-line (`//`): Explains a non-obvious invariant or single line.
- 2Multi-line (`/ ... /`)*: Spans multiple lines or temporarily disables a block.
- 3Javadoc (`/ ... /`)*: Parsed by the
javadoctool to generate HTML API documentation with tags like@param,@return, and@throws.
Unicode System
Java represents char using 16-bit UTF-16, allowing characters from English, Hindi, Japanese, Arabic, and mathematical symbols to be stored and processed uniformly across all operating systems.
Common Mistakes
- Using
_(a single underscore) as an identifier—since Java 9, a lone underscore_is a reserved keyword! - Confusing assignment (
=) with equality comparison (==).
Best Practices
- Write self-documenting identifier names (
totalOrderAmountinstead oftoa). - Use Javadoc on all public APIs and classes so teammates and IDEs see rich hover documentation.
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 0 correct(0 attempted)