Page Stats
Visitor: 303
Java Tokens
Smallest individual units in a program are known as tokens. In simple terms, a Java program is a collection of tokens, comments and white spaces. Java language includes 5 types of tokens. They are:
- Identifiers
- Constants/Literals
- Special Symbols
- Keywords
- Operators
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:
- Identifiers can have alphabets, digits, underscore and dollar sign characters.
- Identifiers must not begin with a digit.
- Uppercase and lowercase letters are distinct.
- 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 easily 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 can not 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 eg: 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 |