Java Tokens
A token is the smallest unit of a Java program that the compiler recognizes as meaningful.
A Java program is made up of a sequence of tokens.
Types of Tokens in Java
1. Keywords
• Definition: Reserved words with predefined meaning in Java.
• Rules: Cannot be used as identifiers (variable names, class names, etc.)
• Examples:
int, float, double, char, class, if, else, while, for, return,
public, static, void, try, catch
2. Identifiers
• Definition: Names created by the programmer for classes, methods, variables, etc.
• Rules:
1. Must start with a letter, $, or _
2. Can contain letters, digits, $, _
3. Cannot be a keyword
• Examples:
age, sum, myVariable, Student, _temp, $value
3. Literals
• Definition: Fixed values directly used in the program.
• Types:
1. Integer literals: 10, -5, 0
2. Floating-point literals: 3.14, -0.5, 2.0
3. Character literals: 'A', 'z', '1'
4. String literals: "Hello", "Java"
5. Boolean literals: true, false
6. Null literal: null
4. Operators
• Definition: Symbols that perform operations on variables or values.
• Types:
1. Arithmetic operators: +, -, *, /, %
2. Relational operators: ==, !=, >, <, >=, <=
3. Logical operators: &&, ||, !
4. Assignment operators: =, +=, -=, *=, /=
5. Increment/Decrement operators: ++, --
6. Ternary operator: ? :
5. Separators
• Definition: Symbols used to define program structure or separate statements.
• Examples:
§ ; → Ends a statement(Statement Terminator)
§ , → Separates items in a list
§ () → Group expressions or method calls
§ {} → Define blocks of code
§ [] → Arrays
6. Punctuators
• Definition: Symbols that punctuate expressions or access members.
• Examples:
§ . → Access members of classes/objects
§ : → Used in for-each loops or labels
§ ? : → Conditional (ternary) operator
Key Points
1. A Java program is a sequence of tokens.
2. Keywords are predefined; identifiers are programmer-defined.
3. Literals represent constant values.
4. Operators perform operations.
5. Separators define the structure of the program.
6. Punctuators organize expressions or access members.