Java Tokens: Comments, Identifiers, Keywords, Separators
Before learning variables, operators, loops, and object-oriented programming in Java, it is important to understand the smallest building blocks of a Java program. These building blocks are called tokens.
Every Java program is made up of words, symbols, names, and punctuation-like characters. The compiler reads these elements and understands the meaning of the program through them. If you do not understand Java tokens properly, even simple Java syntax can become confusing.
What are Java Tokens?
A token in Java is the smallest individual element of a program that has meaning to the compiler. When the Java compiler reads your source code, it breaks the code into small parts and interprets each part separately.
For example, look at this statement:
This line contains multiple tokens:
int→ keywordage→ identifier=→ operator25→ literal;→ separator
So even one simple line of Java code is made up of several tokens.
Main Types of Java Tokens
Java tokens are generally divided into the following categories:
- Keywords
- Identifiers
- Literals
- Operators
- Separators
- Comments
In this tutorial, the main focus is on:
- Comments
- Identifiers
- Keywords
- Separators
1. Comments in Java
Comments are explanatory notes written inside the code for human understanding. Comments are ignored by the compiler, which means they do not affect program execution.
Comments are very useful for:
- explaining logic
- documenting code
- making programs readable
- temporarily disabling code during testing
Types of Comments in Java
Java supports three types of comments:
- Single-line comments
- Multi-line comments
- Documentation comments
Single-Line Comment
A single-line comment starts with //. Everything after // on the same line is treated as a comment.
In the above example, the comment helps explain the purpose of the line.
Multi-Line Comment
A multi-line comment starts with /* and ends with */.
It is used when the comment is long and spans multiple lines.
Documentation Comment
A documentation comment starts with /** and ends with */.
It is used for generating official Java documentation using the javadoc tool.
Why Comments are Important
- They improve readability
- They help teammates understand the code
- They are useful in debugging and maintenance
- They make large projects easier to manage
Common Mistakes with Comments
- Writing too many unnecessary comments
- Writing outdated comments that do not match the code
- Using comments instead of writing clear code
2. Identifiers in Java
An identifier is the name given to program elements such as variables, methods, classes, interfaces, packages, and objects.
In simple words, identifiers are the names programmers create.
Examples of identifiers:
agestudentNamecalculateTotalEmployee
Examples of Identifiers
In the above code:
Studentis a class identifierageis a variable identifiernameis a variable identifiershowDetailsis a method identifier
Rules for Naming Identifiers in Java
Java has strict rules for identifiers. If these rules are not followed, the compiler throws an error.
Rule 1: Identifier can contain letters, digits, underscore, and dollar sign
Rule 2: Identifier cannot start with a digit
Rule 3: Identifier cannot be a keyword
Rule 4: Identifier cannot contain spaces
Rule 5: Java is case-sensitive
This means name, Name, and NAME are all different identifiers.
All three are valid and treated as different variables.
Valid and Invalid Identifiers
| Identifier | Valid or Invalid | Reason |
|---|---|---|
age |
Valid | Starts with a letter |
studentName |
Valid | Uses letters only |
_count |
Valid | Starts with underscore |
$salary |
Valid | Dollar sign allowed |
2marks |
Invalid | Starts with digit |
class |
Invalid | Keyword cannot be used |
student age |
Invalid | Contains space |
Naming Conventions for Identifiers
Although some names may be technically valid, Java developers follow standard naming conventions for readability.
- Class names → PascalCase →
StudentDetails - Method names → camelCase →
calculateTotal() - Variable names → camelCase →
studentAge - Constants → UPPER_CASE →
MAX_LIMIT
3. Keywords in Java
Keywords are reserved words in Java that already have a predefined meaning. These words are part of the Java language syntax and cannot be used as identifiers.
For example:
classintifelsepublicstaticvoid
Examples of Keywords
In this program, these are keywords:
publicclassstaticvoidintif
Commonly Used Java Keywords
Java has many reserved keywords. Here are some important ones:
| Keyword | Purpose |
|---|---|
class |
Used to declare a class |
public |
Access modifier |
private |
Access modifier |
static |
Belongs to class, not object |
void |
No return value |
int |
Integer data type |
double |
Decimal data type |
if |
Conditional statement |
else |
Alternative conditional branch |
return |
Returns a value from method |
new |
Creates object |
this |
Refers to current object |
extends |
Used for inheritance |
implements |
Used to implement interface |
Why Keywords Cannot Be Used as Identifiers
Because keywords already have fixed meaning in Java, using them as variable or class names would confuse the compiler.
Edge Cases Related to Keywords
Case Sensitivity
Java keywords are lowercase. Because Java is case-sensitive, changing the case changes the meaning.
Although Class is technically valid because it is not the lowercase keyword class, it is still poor naming practice.
4. Separators in Java
Separators are symbols used to separate different parts of a Java program. They help define code structure and boundaries.
Common Java separators include:
( )parentheses{ }braces[ ]brackets;semicolon,comma.dot
Examples of Separators
In the above code:
{ }define class and method blocks( )are used in method declaration and method call[ ]are used in array declaration (String[] args);ends statements,separates multiple variables.accesses members likeSystem.out.println
Detailed Understanding of Important Separators
1. Parentheses ( )
Parentheses are used in method definitions, method calls, and control statements.
2. Curly Braces { }
Curly braces define blocks of code such as classes, methods, loops, and conditionals.
3. Square Brackets [ ]
These are mainly used with arrays.
4. Semicolon ;
Semicolon marks the end of a statement in Java.
Forgetting a semicolon is one of the most common beginner errors.
5. Comma ,
Comma is used to separate multiple variables, parameters, or values.
6. Dot .
Dot is used to access members of classes and objects.
Here:
Systemis a classoutis a memberprintlnis a method
Token Breakdown of a Real Java Statement
Let us break down one Java statement into tokens for full understanding.
| Part | Type of Token |
|---|---|
int |
Keyword |
marks |
Identifier |
= |
Operator |
95 |
Literal |
; |
Separator |
Complete Example Using Comments, Identifiers, Keywords, and Separators
Explanation
public,class,static,void,intare keywordsStudentInfo,main,args,age,nameare identifiers// Variable declarationis a single-line comment/* Printing student details */is a multi-line comment{ },( ),[ ],;,.are separators
Common Errors Related to Tokens
Using a Keyword as an Identifier
Starting Identifier with a Digit
Missing Semicolon
This causes a compile-time error because the first statement is not properly terminated.
Using Invalid Characters in Identifier
Best Practices
- Use meaningful identifiers like
studentNameinstead ofs - Do not use confusing names like
ClassorPublic - Use comments only where they add value
- Follow standard naming conventions
- Be careful with separators like semicolon and braces
Interview-Oriented Questions
- What are tokens in Java?
- What is the difference between a keyword and an identifier?
- Can a Java identifier start with a digit?
- Can Java keywords be used as variable names?
- What are separators in Java?
- What is the difference between comments and tokens that affect execution?
- Why is Java considered case-sensitive?
Conclusion
Java tokens are the smallest meaningful units of a Java program. Understanding them is essential because every Java statement is built from tokens. Comments improve readability, identifiers allow naming of program elements, keywords define the language syntax, and separators structure the code properly.
Once you clearly understand tokens, reading and writing Java code becomes much easier. This topic forms the base for almost every other chapter in Java programming.

