Learning outcome: Break Java statements into tokens and correctly identify keywords, identifiers, literals, operators, separators, and comments.
When the Java compiler reads source code, it first breaks the stream of characters into the smallest meaningful units of the language. These units are called tokens. Whitespace and comments help humans read code, but the compiler mostly uses them only to separate tokens.
Java token categories:
| Token category | Purpose | Examples |
|---|---|---|
| Keywords | Reserved words with fixed meaning | class, public, if, while |
| Identifiers | Names created by the programmer | studentName, total, Calculator |
| Literals | Fixed values written directly in code | 25, 'A', "Java", true |
| Operators | Symbols that perform operations | =, +, ==, && |
| Separators | Punctuation that structures code | ;, ,, ., ( ), { }, [ ] |
Tokenizing a Statement
Consider this statement:
int sum = a + 25;
In simple terms: The compiler reads code the way you read a sentence: not as a stream of letters, but as words and punctuation. Each smallest meaningful piece is a token.
Keywords
Keywords are reserved words. They already have a fixed meaning in Java, so you cannot use them as variable names, class names, or method names.
In simple terms: Keywords are words the language has already claimed. Naming a variable class is like naming a child “and” — the sentence stops making sense.
Representative keyword groups (not a complete list):
| Purpose | Examples |
|---|---|
| Data types | byte, short, int, long, float, double, char, boolean, void |
| Control flow | if, else, switch, case, for, while, do, break, continue, return |
| Classes and objects | class, interface, extends, implements, new, this, super, abstract |
| Access and modifiers | public, private, protected, static, final |
| Exception handling | try, catch, finally, throw, throws |
| Packages | package, import |
Note: true, false, and null are technically literals, not keywords, but they are reserved and cannot be used as identifiers.
Modern Java also has context-sensitive words such as record, sealed, permits, and yield, whose special meaning depends on where they appear. Consult the language version used by the course before choosing new identifiers.
Identifiers
An identifier is a name created by the programmer.
In simple terms: An identifier is the name you choose. Java only insists that it is legal; readers insist that it is meaningful, and they are the harder audience to satisfy.
Identifiers are used for:
| Program element | Example |
|---|---|
| Class | StudentRecord |
| Variable | studentName |
| Method | calculateTotal |
| Constant | MAX_MARKS |
| Package | com.cu.project |
Rules enforced by the compiler:
- An identifier may contain Java-recognised Unicode letters and digits, connecting characters such as
_, and currency symbols such as$. - It must not start with a digit.
- It must not be a keyword or reserved literal.
- It is case-sensitive.
- It may be any length, but it should remain readable.
The single underscore _ has been a keyword since Java 9 and cannot be an identifier, although names such as _count remain legal. The dollar sign is legal but is normally reserved for generated code rather than ordinary application names.
Examples:
int totalAmount; // valid
int _count; // valid, though not a preferred style
int student2; // valid
int caféCount; // valid Unicode letters
int 2cool; // invalid: starts with a digit
int class; // invalid: class is reserved
int _; // invalid in Java 9 and later
Professional naming conventions:
| Used for | Style | Example |
|---|---|---|
| Variables and methods | camelCase | studentName, getTotal() |
| Classes and interfaces | PascalCase | StudentRecord, Runnable |
| Constants | UPPER_SNAKE_CASE | MAX_MARKS, PI |
| Packages | lowercase | com.cu.project |
Literals
A literal is a fixed value written directly into source code.
In simple terms: A literal is a value written out in full, exactly as it stands. 42 in the source is the number 42 — nothing is looked up or computed.
| Literal type | Examples | Notes |
|---|---|---|
| Integer | 42, 0x2A, 0b101010, 1_000_000, 100L |
int by default; L makes it long. |
| Floating-point | 3.14, 2.5e3, 3.14f, 3.14d |
double by default; f makes it float. |
| Character | 'A', '9', '\n', '\u0041' |
Single quotes hold one character. |
| String | "Hello", "Chandigarh University" |
Double quotes create a String object. |
| Boolean | true, false |
Used in conditions and logical expressions. |
| Null | null |
Means no object reference. |
Example:
int marks = 95;
long population = 1_400_000_000L;
double price = 99.50;
float rate = 2.5f;
char grade = 'A';
String course = "Project Based Learning in Java";
boolean passed = true;
String middleName = null;
Operators as Tokens
Operators perform actions on values.
Common operator groups:
| Group | Examples |
|---|---|
| Arithmetic | +, -, *, /, % |
| Assignment | =, +=, -=, *=, /=, %= |
| Relational | ==, !=, <, >, <=, >= |
| Logical | &&, ||, ! |
| Increment/decrement | ++, -- |
| Conditional | ?: |
Operators are covered in depth in the operators and casting topic.
Separators
Separators give structure to code.
| Separator | Use |
|---|---|
; |
Ends a statement. |
, |
Separates items in a list. |
. |
Accesses members and separates package names. |
( ) |
Holds method parameters and groups expressions. |
{ } |
Holds class, method, and control blocks. |
[ ] |
Declares arrays and accesses array elements. |
Example:
public void greet(String name) {
System.out.println("Hello, " + name);
}
Comments
Comments are ignored by the compiler and are written for humans.
// Single-line comment
/*
Multi-line comment
spanning several lines
*/
/**
* Javadoc comment used to generate documentation.
*/
Use comments to explain intent, decisions, or non-obvious logic. Avoid comments that merely repeat the code.
Common Mistakes
| Mistake | Why it is wrong |
|---|---|
int class = 5; |
class is a keyword. |
int 2marks = 90; |
Identifiers cannot start with a digit. |
String name = 'Aarav'; |
Strings use double quotes, not single quotes. |
char grade = "A"; |
A char uses single quotes and holds one character. |
int total amount; |
Spaces are not allowed inside identifiers. |
int _ = 1; |
A single underscore is a keyword in Java 9 and later. |
Practice
Problem 1: Identify the Tokens
Break the statement into token categories:
double area = 3.14 * radius * radius;
Problem 2: Valid or Invalid
Mark each identifier as valid or invalid and give a reason:
myVar
2cool
_temp
class
totalAmount
MAX_MARKS
student-name
Problem 3: Rewrite with Conventions
Improve these names:
studentname
CalculateTotal
maxmarks
Student_record
Quick Check
1. What are the five token categories in Java?
Java has five token categories: keywords, identifiers, literals, operators, and separators.
2. Why can class not be used as a variable name?
class is a reserved keyword with a fixed meaning in Java, so it cannot be used as an identifier.
3. What is the difference between 'A' and "A"?
'A' is a char literal holding one character. "A" is a String object containing one character.
4. Which identifier style is used for constants?
Constants use uppercase letters with underscores, also called UPPER_SNAKE_CASE, for example MAX_MARKS.
5. Are true, false, and null keywords or literals?
They are literals, not keywords, but they are reserved and cannot be used as identifiers.
Lecture Quiz
1. Which statement is true? (a) new and delete are Java keywords. (b) try, catch, and thrown are Java keywords. (c) static, unsigned, and long are Java keywords. (d) exit, class, and while are Java keywords. (e) return, static, and default are Java keywords.
Answer: (e). return, static, and default are Java keywords. delete, thrown, unsigned, and exit are not Java keywords.
2. Java is a ........... language: weakly typed, strongly typed, moderate typed, or none of these?
Java is strongly typed. Every variable has a declared type, and type rules are checked by the compiler.
3. In Java, byte, short, int, and long are signed or unsigned?
They are signed integer types. They can represent both negative and positive values.
Summary
Tokens are the building blocks of Java source code. Keywords have fixed meanings, identifiers are programmer-created names, literals are fixed values, operators perform actions, and separators structure the program.