What are tokens in Java?

Java tokens are the smallest meaningful units in a program that the compiler can understand. They are the basic building blocks of Java code, such as keywords, identifiers, literals, operators, and separators.

Quick Scoop

Think of a Java program like a sentence: tokens are the individual words and symbols that make it up. The compiler reads these pieces one by one during lexical analysis, which is the first step in understanding the code.

Main types of tokens

  • Keywords : Reserved words with fixed meaning, like class, public, if, and int.
  • Identifiers : Names you create for variables, methods, classes, and objects, like sum, main, or Calculator.
  • Literals : Fixed values written directly in code, like 10, 3.14, or "Hello".
  • Operators : Symbols that perform operations, like +, -, =, *, and /.
  • Separators : Symbols that structure code, like (, ), {, }, ;, and ,.

Example

In this line: int number = 10; the tokens are:

  • int β†’ keyword
  • number β†’ identifier
  • = β†’ operator
  • 10 β†’ literal
  • ; β†’ separator

Important note

Comments and whitespace are not counted as tokens for the compiler’s core meaning, even though they help humans read the code.

In one sentence

Tokens are the tiny code pieces Java uses to recognize structure and meaning before it compiles the program.

Meta description: Learn what tokens in Java are, their five main types, and a simple example that shows how the compiler reads them.