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:
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:
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 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;
The following special symbols are used in Java having some special meaning and thus, cannot be used for some other purpose.
Escape Sequence | Description |
---|---|
\' | Single Quotation Mark |
\" | Double Quotation Mark |
\\ | Back slash |
\t | Tab |
\b | Back space |
\r | Carriage Return |
\n | New line |