Java Syntax · Lesson 1/9
11%
⏱ 5 min read Modified: 2026-07-03

Java Language Lexicon: Whitespace, Identifiers, Keywords & Comments

In today's lesson, we will start exploring the lexicon of the Java language and understand the components of a Java program.

1. Whitespace

Let's begin by examining whitespace.

Java is a free-form language, meaning there are no strict rules regarding whitespace when writing a program.

In Java, whitespace includes spaces, tabs, and newlines. You can insert multiple spaces, tabs, or line breaks, and the code will still compile. The only requirement is at least one space between lexemes that are not already separated by an operator or delimiter.

2. Identifiers

Next, let's discuss identifiers. What is an identifier? Identifiers are used to name classes, methods, or variables. For example, in the following program, MyFirstApp is the class name (identifier), main is a method identifier, and args is also an identifier:

public class MyFirstApp {
    public static void main(String[] args) {
        System.out.println("Hello world!!!");
    }
}

There are specific rules for choosing valid identifiers:

  • An identifier can consist of letters (uppercase and lowercase), digits, underscores (_), and dollar signs ($).
  • However, an identifier CANNOT start with a digit.
  • Java is case-sensitive.
  • Valid identifiers: MinTemp, sum, x4, $test, my_number.
  • Invalid identifiers: 3min, min-temp, yes/no.
  • Keywords cannot be used as identifiers.

3. Keywords and Reserved Words

What are keywords? Keywords are words the language has reserved for a special meaning. Together with operators and delimiters, they form the foundation of the Java language. All keywords are written in lowercase, and none of them can be used as an identifier — doing so causes a compilation error.

It's convenient to split them into three groups: keywords, reserved literals, and contextual keywords. Let's look at each.

Keywords

These are the reserved words of the language. They cannot be used as identifiers:

You don't need to memorize them — you'll meet each one throughout the course. Note two special cases: const and goto are reserved but not used in the language. They're kept so the compiler can produce a clear error if you try to use them.

Reserved Literals

These are special values. Technically they aren't keywords, but they are reserved in the same way and cannot be used as identifiers:

  • true
  • false
  • null

Contextual Keywords

Contextual keywords have a special meaning only in a specific context (for example, when declaring a module or a sealed class); elsewhere they can technically be used as identifiers. The compiler won't reject a variable named after one of them, but doing so is not recommended — it makes the code confusing.

  • var
  • yield
  • record
  • sealed
  • permits
  • non-sealed
  • when
  • module
  • open
  • opens
  • requires
  • exports
  • transitive
  • uses
  • provides
  • with
  • to

The first group (var, yield, record, sealed, permits, non-sealed, when) relates to class and expression syntax; the second (module, requires, exports, and the rest) belongs to the module system introduced in Java 9.

4. Comments

Comments are parts of the code ignored by the compiler, used to provide explanations and descriptions to make the code more readable. They help document functionality, logic, and key parts of the code.

There are three types of comments: single-line, multi-line, and documentation comments.

Single-Line Comments

In IntelliJ IDEA, commented-out code appears in gray:

// int avgTemp = 1;

Multi-Line Comments

You can comment out multiple lines of code using /* ... */.

/* public static long roundTime(long time) {
        return time / THOUSAND * THOUSAND;
 }*/

Documentation Comments

These comments are typically used for classes or methods and can be converted into documentation.

/**
  * Created by Tatyana on 07.04.2017.
  */

5. Delimiters

Java uses the following delimiters:

  • Parentheses ( ): Used for method parameters, type casting, and expressions.
  • Curly braces { }: Define classes, methods, and code blocks.
  • Square brackets [ ]: Define arrays and access elements.
  • Semicolon ;: Ends statements.
  • Comma ,: Separates variable identifiers and constructs loops.
  • Dot .: Separates package names, sub-packages, classes, variables, and methods.

As you can see, delimiters are crucial for structuring Java programs.

Frequently Asked Questions

How much whitespace is allowed between lexemes in Java?

As much as you like. Java is a free-form language: you can place one or more spaces, tabs, or line breaks between lexemes and the code still compiles. The only rule is that there must be at least one space between two lexemes that aren't already separated by an operator or a delimiter. For example, int x can't be written as one word, but a+b is fine because the operator separates them.

What's the difference between a keyword, a reserved literal, and a contextual keyword?

Keywords (such as class, if, int) are permanently reserved and can never be identifiers. Reserved literals are true, false, and null: technically not keywords, but reserved in the same way. Contextual keywords (such as var, record, sealed, module) have a special meaning only in a specific place in the code; elsewhere they can technically be identifiers — though using them that way isn't recommended.

Can I name a variable var or record?

Technically yes — these are contextual keywords, and the compiler won't reject a variable with that name. But it's a bad idea: the code becomes confusing and readers may mistake the variable name for special syntax. Real keywords like class or int, however, cannot be used as names — that's a compilation error.

Should I leave commented-out code in my program?

Usually not. If code is commented out, it isn't needed — and most likely won't be needed later. It's better to delete it: a version control system (Git) keeps the change history, so you can always restore an old version if necessary. Commented-out fragments are common in real projects, but they're a drawback rather than good practice.

Comments

Please log in or register to have a possibility to add comment.

‹ Previous lesson Next lesson ›