HexLab

Lesson 4

Tokens

A focused lesson on the smallest units used in Java source code.

Lesson content

Tokens

Java Tokens

Java tokens are the smallest individual units in a program.

Keywords

Keywords are predefined reserved words in Java. Each keyword has a special meaning and is always written in lowercase.

  • class
  • while
  • for
  • interface
  • abstract

Identifiers

Identifiers are used to name variables, constants, functions, classes, and arrays. They are usually defined by the user and must be different from reserved keywords.

  • The first character must be a letter, underscore, or dollar sign.
  • An identifier cannot start with digits, but it may contain digits later.
  • Whitespace cannot be included.
  • Identifiers are case sensitive.

Valid examples include PhoneNumber, PRICE, radius, a, and a1. Invalid examples include 1sum and Sum 1.

Literals

A literal is a notation that represents a fixed value in source code. Once defined, it cannot be changed.

  • Integer
  • Floating Point
  • Character
  • String
  • Boolean

Operators

Operators are special symbols used to perform mathematical operations or logical manipulations.

  • Arithmetic Operators
  • Assignment Operators
  • Relational Operators
  • Unary Operators
  • Logical Operators
  • Ternary Operators
  • Bitwise Operators
  • Shift Operators

Separators

Separators are also known as punctuators in Java.

  • ; semicolon
  • , comma
  • . dot
  • { } braces
  • [ ] bracket
  • ( ) parenthesis

Comments

Comments are used to describe the program. Java supports single-line comments and multi-line comments.

  • Single line: // comment
  • Multi-line: /* comment */

Constants

Constants are fixed values that do not change during execution.

  • Integer constants such as 111 and 1234.
  • Floating point constants such as 223.14 and 400.054.
  • Character constants such as A, m, and 9.
  • String constants such as Welcome and Hello.