Java
Module 2: Java Fundamentals, Variables & Data TypesBeginner20 min read

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.

Key Takeaways & Core Concepts
  • 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.

Syntax, Javadoc & Unicode Characters
1/**
2 * Demonstrates Java syntax rules and UTF-16 Unicode support.
3 */
4public class Main {
5 public static void main(String[] args) {
6 int studentCount = 42; // camelCase identifier
7 char rupeeSymbol = '\u20B9';
8 char alpha = '\u03B1';
9
10 System.out.println("Active students: " + studentCount);
11 System.out.println("Currency: " + rupeeSymbol + " | Greek Alpha: " + alpha);
12 }
13}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
13 lines
Note: `\u20B9` and `\u03B1` are 16-bit Unicode escape sequences evaluated by the Java compiler.

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., 2ndUser is invalid; user2 is valid).
  • Case-sensitive (studentCount and StudentCount are 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

  1. 1
    Single-line (`//`): Explains a non-obvious invariant or single line.
  2. 2
    Multi-line (`/ ... /`)*: Spans multiple lines or temporarily disables a block.
  3. 3
    Javadoc (`/ ... /`)*: Parsed by the javadoc tool 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 (totalOrderAmount instead of toa).
  • Use Javadoc on all public APIs and classes so teammates and IDEs see rich hover documentation.
Practice & AssessmentJava Programming Tutorial

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)