BCA / B.Tech 8 min read

Tokens in Java

Tokens in Java:

A token is the smallest individual unit of a program that is meaningful to the compiler. When you write a Java program, the compiler breaks the source code down into a sequence of these tokens during the lexical analysis phase.

Types of Tokens in Java:
  • Keywords: These are reserved words that have a special, predefined meaning in the Java language (e.g., `class`, `public`, `static`, `void`, `if`, `else`, `for`). They cannot be used as variable names.
  • Identifiers: These are user-defined names given to variables, methods, classes, packages, and interfaces (e.g., `myVariable`, `main`, `MyClass`).
  • Literals: These are constant values that are assigned directly in the code (e.g., `100`, `3.14`, `A`, `"Hello World"`, `true`).
  • Operators: These are symbols that perform operations on operands (e.g., `+`, `-`, `*`, `/`, `=`, `==`, `&&`).
  • Separators: These are symbols used to define the structure of the code and separate other tokens (e.g., `( )`, `{ }`, `[ ]`, `;`, `,`, `.`).