Home » Java » Tokens in Java

Tokens in Java

  • The smallest individual unit of the program is called a token.
  • Every statement is an organized collection of Java tokens. Every java statement is terminated by; a sign.
  • Every java program is an organized collection of statements/instructions.

Types of Tokens in Java

We have the following types of tokens in Java:

  • Constant
  • Identifier
  • Keywords
  • Operator
  • Separator

Constant Tokens

A value is called a constant. They are also called literals. A constant can never be changed.

Integer

Any number without a decimal point is called an integer. They may be positive, negative, or zero.

They can be represented in the following number system in java:

  • Binary Integer: A binary integer is a combination of 1 and 0. The binary literals are preceded with 0b. Eg: 0b10110
  • Octal Integer: A octal integer is a combination of digits 0 to 7. The octal literals are preceded with 0. Eg: 0123
  • Decimal Integer: A decimal integer is a combination of digits 0 to 9. Eg: 12345
  • Hexadecimal Integer: A Hexadecimal integer is a combination of decimal digits 0 to 15.
    The decimal 10 to 15 is represented by letters A to Z(small a to z can also be used) in hexadecimal.

The hexadecimal literals begin with 0x. Eg: 0x123A

Real

Any number with a decimal point is called a real value.

223.5, -123.4, 0.5, .121, 0.0 etc.

Single Character

A single alphabet, digit, or symbol which is enclosed inside single quotes is called a single character constant. Java supports Unicode characters.

'x', '?', '8' etc.

String

Anything enclosed in double quotes is called string. A string can contain a combination of alphabets, digits, or symbols.

"vermatarun", "777123", "tarun@71234", "212@3#$#<"

Note: An empty pair of double quotes is also a valid string in java, Eg: ""

Boolean

Boolean values are true and false.

Identifier Tokens

Any name used in our program is called an identifier. Eg: variable name, interface name, class name, function name, package name, etc.

We must follow certain rules while creating identifier:

  • Identifier can contain alphabets, digits, dollar ($), and underscore (_) symbol.
  • Except for dollar and underscore we cannot use any special symbol in identifier.
  • Identifier can be started with underscore or dollar sign.
  • Identifier cannot be started with a digit.
  • Keywords cannot be used as identifiers.

Note: We can use both small as well as capital letters in identifiers.

Keyword Tokens

  • Keywords are special words that have their predefined work in java.
  • Keywords cannot be used as identifiers.
  • We have 48 keywords in java.
  • We also have two reserved words const and goto in java, which do not have any work but cannot be used as an identifier.

Note: Most of the keywords are in small letters only.

Operator Tokens

Operators are special symbols or keywords which are used to operate on operands and generate the result.

Eg. 501+200, here + is an operator which performs the addition of 501 and 200.

Separator Tokens

The separator is a symbol that is used to separate two tokens.

Consider the following example:

int a;

In the above statement int is a keyword and a is an identifier, and they are separated by space, so space is a separator.