Java Tokens: Comments, Identifiers, Keywords, Separators

Java 8 min min read Updated: Mar 30, 2026 Beginner
Java Tokens: Comments, Identifiers, Keywords, Separators
Beginner Topic 2 of 25

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.

Key Concept: A token is the smallest meaningful unit in a Java program. Examples include keywords, identifiers, literals, operators, separators, and comments used for readability.

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:

java int age = 25;

This line contains multiple tokens:

  • int → keyword
  • age → identifier
  • = → operator
  • 25 → 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.

java // This is a single-line comment int age = 20; // storing age

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.

java /* This is a multi-line comment. It can be used to explain longer code logic. */ int marks = 95;

Documentation Comment

A documentation comment starts with /** and ends with */. It is used for generating official Java documentation using the javadoc tool.

java /** * This method adds two numbers. * @param a first number * @param b second number * @return sum of a and b */ int add(int a, int b) { return a + b; }

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
Best Practice: Use comments to explain “why” something is done, not to explain obvious code that already speaks for itself.

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:

  • age
  • studentName
  • calculateTotal
  • Employee

Examples of Identifiers

java class Student { int age; String name; void showDetails() { System.out.println(name + " - " + age); } }

In the above code:

  • Student is a class identifier
  • age is a variable identifier
  • name is a variable identifier
  • showDetails is 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

java int age; int age2; int _count; int $salary;

Rule 2: Identifier cannot start with a digit

java int 2age; // Invalid int age2; // Valid

Rule 3: Identifier cannot be a keyword

java int class; // Invalid because class is a keyword int marks; // Valid

Rule 4: Identifier cannot contain spaces

java int student age; // Invalid int studentAge; // Valid

Rule 5: Java is case-sensitive

This means name, Name, and NAME are all different identifiers.

java int name = 10; int Name = 20; int NAME = 30;

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
Interview Point: Rules define what is allowed, while naming conventions define what is recommended.

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:

  • class
  • int
  • if
  • else
  • public
  • static
  • void

Examples of Keywords

java public class Demo { public static void main(String[] args) { int number = 10; if (number > 5) { System.out.println("Greater than 5"); } } }

In this program, these are keywords:

  • public
  • class
  • static
  • void
  • int
  • if

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.

java int if = 10; // Invalid int number = 10; // Valid

Edge Cases Related to Keywords

Case Sensitivity

Java keywords are lowercase. Because Java is case-sensitive, changing the case changes the meaning.

java int class = 10; // Invalid int Class = 10; // Valid, but confusing and not recommended

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

java public class Demo { public static void main(String[] args) { int a = 10, b = 20; System.out.println(a + b); } }

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 like System.out.println

Detailed Understanding of Important Separators

1. Parentheses ( )

Parentheses are used in method definitions, method calls, and control statements.

java if (a > b) { System.out.println("A is greater"); }

2. Curly Braces { }

Curly braces define blocks of code such as classes, methods, loops, and conditionals.

java class Test { void show() { System.out.println("Inside method"); } }

3. Square Brackets [ ]

These are mainly used with arrays.

java int[] numbers = {1, 2, 3, 4};

4. Semicolon ;

Semicolon marks the end of a statement in Java.

java int x = 100; System.out.println(x);

Forgetting a semicolon is one of the most common beginner errors.

5. Comma ,

Comma is used to separate multiple variables, parameters, or values.

java int x = 10, y = 20, z = 30;

6. Dot .

Dot is used to access members of classes and objects.

java System.out.println("Hello");

Here:

  • System is a class
  • out is a member
  • println is a method

Token Breakdown of a Real Java Statement

Let us break down one Java statement into tokens for full understanding.

java int marks = 95;
Part Type of Token
int Keyword
marks Identifier
= Operator
95 Literal
; Separator

Complete Example Using Comments, Identifiers, Keywords, and Separators

java public class StudentInfo { public static void main(String[] args) { // Variable declaration int age = 21; String name = "Amit"; /* Printing student details */ System.out.println("Name: " + name); System.out.println("Age: " + age); } }

Explanation

  • public, class, static, void, int are keywords
  • StudentInfo, main, args, age, name are identifiers
  • // Variable declaration is 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

java int for = 10; // Error

Starting Identifier with a Digit

java int 1value = 100; // Error

Missing Semicolon

java int age = 20 System.out.println(age);

This causes a compile-time error because the first statement is not properly terminated.

Using Invalid Characters in Identifier

java int student-name = 50; // Error because hyphen is not allowed

Best Practices

  • Use meaningful identifiers like studentName instead of s
  • Do not use confusing names like Class or Public
  • 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.

Quick Summary: Java tokens are the basic building blocks of Java code. Comments help humans read code, identifiers name program elements, keywords have predefined meaning, and separators define code structure.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators