Ad

Java Tokens

In Java, tokens are the smallest individual units in a program. In simple terms, a Java program is a collection of tokens, comments, symbols, white spaces, etc. Java language includes 5 types of tokens. They are:

  1. Identifiers
  2. Constants/Literals
  3. Special Symbols
  4. Keywords
  5. Operators

Java Identifiers

Identifiers are the name, designed by the programmer. Identifiers are used for naming classes, methods, variables, objects, labels, packages and interfaces in a program. Java identifiers follow the following rules:

  1. Identifiers can have alphabets, digits, underscore and dollar sign characters.
  2. Identifiers must not begin with a digit.
  3. Uppercase and lowercase letters are distinct.
  4. Identifiers can be of any length.

Identifier must be meaningful, short enough to be quickly and easily typed and long enough to be descriptive and easy to read.

Example of valid identifiers: sum, sum2, average, totalMarks, PRINCIPAL_AMOUNT, $result, sum_of_array.

Example of invalid identifiers: total Marks, 2sum, sum-2, sum&array

Constants/Literals

Constants are like normal variables, only difference is that their values cannot be modified in the program once they are defined. Constants refer to the fixed value. They are also known as literals. Constants may belong to any of the data type: Integer, Floating point, Character, String, Boolean

final data_type variable_name;

Special Symbols

The following special symbols are used in Java having some special meaning and thus, cannot be used for some other purpose.

  • Brackets [] Opening and closing brackets are used as array element. It is use for 1D and 2D array.
  • Parentheses () These special symbols are used to indicate function calls and function parameters.
  • Braces {} These opening and ending curly braces marks the start and end of a block of code containing more than one executable statement.
  • comma (,) It is used to separate more than one statements for e.g.: separating parameters in function calls, declaring multiple variables.
  • semicolon It is used for ending statement, it is also known as statement terminator.

Java Escape Sequences

Escape Sequence Description
\' Single Quotation Mark
\" Double Quotation Mark
\\ Back slash
\t Tab
\b Back space
\r Carriage Return
\n New line