Java Tokens Explained: Keywords, Identifiers, Literals & Operators
Java tokens are the smallest individual units of a Java program. Every Java source file is built by combining tokens, comments, and white spaces. Understanding tokens is the first step to mastering Java syntax.
In this guide, you will learn the five types of tokens in Java — reserved words, identifiers, literals, operators, and separators — with rules, examples, and valid/invalid cases.
What are Java Tokens?
- Java tokens are the smallest unit of a Java program.
- A Java program constructs expressions and statements using tokens.
- A Java program is a group of different types of tokens, comments, and white spaces.
There are five types of tokens in Java:
- Reserved Words
- Identifiers
- Literals
- Operators
- Separators
1. Reserved Words
- Every language provides some reserved words for the compiler to know the functionalities defined by that language.
- Every reserved word has a special meaning that is understood by the compiler.
- Reserved words are divided into two categories:
- Keywords — define the functionalities.
- Literals — define values.
- There are around 50 keywords and 3 literals in Java.
- Keywords can never be used as identifiers because the compiler treats them as reserved words.
The following table lists the reserved words in Java:
| abstract | assert | boolean | break | byte |
| case | catch | char | class | continue |
| default | do | double | else | enum |
| extends | final | finally | float | for |
| if | implements | import | instanceof | int |
| interface | long | native | new | package |
| private | protected | public | return | short |
| static | strictfp | super | switch | synchronized |
| this | throw | throws | transient | try |
| void | volatile | while | true | false |
| null |
Note: true, false, and null are
technically literals, but they are reserved and cannot be used as identifiers.
var, record, sealed, permits, and
yield are contextual keywords in modern Java — they are only reserved
in specific contexts.
2. Identifiers
- Identifiers are used to identify things.
- Identifiers are used with classes, methods, variables, interfaces, and enums.
Valid Identifiers
- An identifier should start with a letter.
- An identifier can start with a
$symbol. - An identifier can start with an underscore (
_). - Identifiers are case-sensitive —
intandIntare different. - Class names should use PascalCase — e.g.,
MyClass,HelloWorldExample. - Method names should use camelCase — e.g.,
get(),getName(),getByRoll(). - Variable names should start with a lowercase letter — e.g.,
roll,rollNumber.
Invalid Identifiers
- Identifiers never start with a number.
- Identifiers never contain special symbols except
$and_. - Identifiers never contain a hyphen (
-). - Identifiers never contain a space.
- Identifiers can never be a reserved word.
- Identifiers never contain operators like
+,-,*,/.
Examples:
- Valid:
age,$amount,_value,myVariable,MyClass - Invalid:
123abc,my-var,my var,class,a+b
3. Literals
A literal is any constant value assigned to a variable.
There are five types of literals in Java:
- Integral Literals
- Float Literals
- Char Literals
- String Literals
- Boolean Literals
1. Integral Literals
There are four ways to use integral literals:
i. Decimal Literals (Base 10)
- Allowed digits:
0–9. - Example:
int a = 121; int b = 101; int c = 643943;
ii. Octal Literals (Base 8)
- Octal numbers always start with
0. - Allowed digits:
0–7. - Example:
int a = 0121; int b = 0101; int c = 064343;
iii. Hexadecimal Literals (Base 16)
- Allowed digits:
0–9and lettersa–f(lowercase or uppercase). - Hexadecimal numbers always start with
0xor0X. - Example:
int a = 0x121; int b = 0X101; int c = 0x6d343;
iv. Binary Literals
- Allowed digits:
0and1. - Binary literals must be prefixed with
0bor0B. - Example:
int a = 0b101; int b = 0B0101; int c = 0b111;
2. Float Literals
- Only decimal digits
0–9are allowed. - You cannot specify octal or hexadecimal in float literals.
- An octal-looking value is treated as decimal here.
- Example:
float a = 101.10f; float b = 0101.9f;(0101acts as decimal101). - Invalid:
float c = 0X1.4f;— hexadecimal is not allowed.
3. Char Literals
There are four ways to specify char literals:
i. Single Quote
- Only a single character inside single quotes.
- Example:
char a = 'a'; char b = '1';
ii. Integral Literals
- Integral literals can be used to represent Unicode values.
- They can be specified in decimal, octal, or hexadecimal.
- The Unicode range starts from
0to65535. - Example:
char a = 012; char b = 101; char c = 0x2f;
iii. Unicode Literals
- Specified using
'\uxxxx'wherexxxxis four hexadecimal digits. - Example:
char a = '\u0000'; char b = '\u0076';
iv. Escape Literals
- Escape characters are used as char literals.
- Example:
char a = '\t'; char b = '\n';
4. String Literals
- A String is a group of characters.
- String literals are always represented with double quotes (
""). - Example:
String name = "Deep Singh"; - Example:
String msg = "Welcome to \n deepsingh44.blogspot.com";
Note: Since Java 15, Text Blocks (using triple quotes
""") allow multi-line string literals without escape characters — useful for
SQL, JSON, and HTML strings.
5. Boolean Literals
- Boolean literals support only two values:
trueandfalse. - Example:
boolean status = true; boolean a = false;
There is also a special literal null, which represents the absence of a value
and can be assigned to any reference type.
4. Operators
Operators are used to perform mathematical or logical operations.
There are nine types of operators in Java:
- Unary Operators
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Ternary Operators
- Assignment Operators
- Instanceof Operators
- Shift Operators
5. Separators / Punctuators
Separators (also called punctuators) define the structure of Java code — they tell the compiler how statements, blocks, and expressions are organized.
| Punctuator | Name | Description | Example |
|---|---|---|---|
() |
Parentheses | Used for expressions, parameters, and arguments. | int i = (1 + 3 / 5) + 2;void add(int i, int j) {}add(1, 2); |
{} |
Curly Braces | Used for blocks, class bodies, and array initializers. | void add(int i, int j) {}int[] rolls = {1, 2, 3, 4}; |
[] |
Square Brackets | Used for array declaration and access. | int[] rolls = {1, 2, 3, 4}; |
, |
Comma | Used to separate parameters, arguments, and values. | void add(int i, int j) {}int[] rolls = {1, 2, 3, 4}; |
: |
Colon | Used for switch cases, enhanced for loop, and ternary operator. | switch (x) { case 1: ... }for (String s : list) {}cond ? right : wrong; |
; |
Semicolon | Used to terminate statements. | System.out.println("Hello World"); |
... |
Var-args | Used for variable-length arguments. | void add(int i, int... j) {} |
Frequently Asked Questions
What are Java tokens?
Java tokens are the smallest individual units of a Java program. A Java program is built by combining tokens, comments, and white spaces. The five types of tokens in Java are reserved words, identifiers, literals, operators, and separators.
What is the difference between keywords and literals in Java?
Keywords define functionalities and control the structure of the program
(e.g., class, if, while).
Literals represent fixed values assigned to variables (e.g.,
100, 3.14, 'A', true,
null). Both are reserved words in Java.
What are the rules for Java identifiers?
A Java identifier must start with a letter, dollar sign ($), or underscore
(_). It cannot start with a digit. It cannot contain spaces, hyphens, or
special symbols (except $ and _). It cannot be a reserved word.
Identifiers are case-sensitive, so int and Int are different.
What are the types of literals in Java?
Java supports five types of literals: integral literals (decimal, octal,
hexadecimal, binary), floating-point literals, char
literals (single quote, integral, Unicode, escape), string
literals, and boolean literals (true or
false). There is also the null literal.
What are separators in Java?
Separators (also called punctuators) are symbols that define the structure of Java code.
Common separators include parentheses (), curly braces {},
square brackets [], comma ,, colon :, semicolon
;, and var-args ....
Conclusion
Java tokens are the building blocks of every Java program. Mastering reserved words, identifiers, literals, operators, and separators is essential before moving on to expressions, control flow, and object-oriented programming.
Practice writing small programs that use all five types of tokens, and pay close attention to identifier naming rules — they will keep your code clean and readable.
Related topics: Java Variables and Data Types, Java Operators with Examples, Java Identifier Naming Conventions, Java Keywords List, Java Literals in Detail
1 comment:
Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care. We are providing the best services click on below links to visit our website.
Oracle Fusion HCM Training
Workday Training
Okta Training
Palo Alto Training
Adobe Analytics Training
Post a Comment