Java Language Essentials:


Tokens, Identifiers, Literals, Keywords, and Operators Explained
Before diving into writing complex Java programs, every developer must understand the core language elements โ the building blocks of the Java language. These elements ensure your code is understood by the compiler, follows best practices, and avoids errors early on.
In this blog, weโll walk through:
โ Tokens & Lexemes
๐ท๏ธ Identifiers & Naming Rules
๐ข Literals & Number Systems
๐ Keywords & Reserved Words
โ Operators in Java
Letโs explore each one in detail.
๐งฉ 1. Tokens and Lexemes
In Java, a lexeme is the smallest unit of code โ like a word in a sentence.
A token is a group of lexemes that belong to the same category โ such as data types, identifiers, operators, etc.
๐งช Code Example:
int a = b + c * d;
Explanation:
int
โ Data typea
โ Variable name (identifier)=
โ Assignment operatorb
,c
,d
โ Identifiers+
,*
โ Arithmetic operators;
โ Terminator (special symbol)
So, this line contains 9 lexemes, which are categorized into tokens:
Data type:
int
Identifiers:
a
,b
,c
,d
Operators:
=
,+
,*
Special symbol:
;
๐ท๏ธ 2. Identifiers and Naming Rules
An identifier is the name you assign to variables, classes, methods, etc.
โ Valid Identifiers:
int empNo = 101;
String _name = "Java";
float $salary = 55000.0f;
Explanation:
empNo
,_name
, and$salary
follow Javaโs naming rules:Start with a letter,
_
, or$
Do not use special characters like
@
or-
Do not begin with numbers
โ Invalid Identifiers:
int 9value = 10; // โ Starts with a number
String emp@name = "X"; // โ Special character '@' not allowed
float salary.amount = 1000f; // โ Dot (.) not allowed in names
Java will throw a compilation error for each of these cases.
โ ๏ธ Scope Rule Example:
class Test {
int x = 10; // Global variable
void method() {
int x = 20; // Local variable with the same name
System.out.println(x); // Prints 20
}
}
Explanation: Java allows reuse of identifier names in nested scopes, but not in the same scope.
๐ข 3. Literals and Number Systems
A literal is a constant value used directly in the code.
โ Example:
int age = 25;
char grade = 'A';
boolean isActive = true;
String name = "Java";
Explanation:
25
โ Integer literal'A'
โ Character literaltrue
โ Boolean literal"Java"
โ String literal
๐งฎ Number Systems in Java
System | Prefix | Valid Example | Explanation |
Binary | 0b /0B | int x = 0b1010; | Represents binary 1010 โ decimal 10 |
Octal | 0 | int x = 0754; | Represents octal 754 โ decimal 492 |
Decimal | (none) | int x = 100; | Regular base-10 number |
Hexadecimal | 0x /0X | int x = 0x1A3; | 1A3 โ 419 in decimal |
๐ Java 7 Feature:
int creditCard = 1234_5678; // Improved readability
Underscores can be used in numeric literals to visually separate digits. Java will ignore _
internally.
๐ 4. Keywords vs Reserved Words
โ Keywords:
These are reserved by Java and have specific meanings in the language.
public class Student {
private int id;
static final double PI = 3.14;
}
Explanation:
public
,class
,private
,static
, andfinal
are keywords.These cannot be used as variable names.
๐ Reserved Words:
These are not used in Java yet, but they are reserved for future use.
goto, const
If you try to use these in code, the compiler will throw an error.
โ 5. Operators in Java (with Code Explanation)
Operators are symbols that perform operations on variables and values.
๐ธ Arithmetic Operators
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3
System.out.println(a % b); // 1 (Remainder)
๐ธ Unary Operators
int x = 5;
System.out.println(++x); // 6 (Pre-increment)
System.out.println(x--); // 6 (Post-decrement, then x becomes 5)
System.out.println(x); // 5
๐ธ Comparison (Relational) Operators
int a = 10, b = 20;
System.out.println(a < b); // true
System.out.println(a == b); // false
Explanation: Used in conditions like if
, while
, etc.
๐ธ Logical Operators
int age = 25;
boolean result = (age > 18 && age < 60);
System.out.println(result); // true
Explanation: &&
returns true if both sides are true.
๐ธ Bitwise Operators
int a = 10; // 1010
int b = 2; // 0010
System.out.println(a & b); // 2 (Bitwise AND)
System.out.println(a | b); // 10 (Bitwise OR)
System.out.println(a ^ b); // 8 (Bitwise XOR)
๐ธ Shift Operators
System.out.println(a << 2); // 40 (Left shift by 2 bits)
System.out.println(a >> 2); // 2 (Right shift by 2 bits)
Explanation:
a << 2
: Moves bits left and adds zeros โ multiplies by 2ยฒ = 4a >> 2
: Moves bits right โ divides by 2ยฒ = 4
๐ธ Ternary Operator
int marks = 85;
String result = (marks >= 50) ? "Pass" : "Fail";
System.out.println(result); // Pass
Explanation: Short form of if-else
โ Final Summary
Concept | Why It Matters |
Tokens | Breaks source code into meaningful units |
Identifiers | Allows naming variables, classes, methods |
Literals | Represents constant data directly in code |
Number Systems | Supports multiple numeric formats (binary, octal, etc.) |
Keywords | Reserved for language syntax |
Operators | Core to performing computations and logic |
๐ง Up Next:
In the next blog, weโll cover:
๐งฌ Java Type Casting
๐ Java Statements and Flow Control (if, switch, loops)
๐ฏ Type Promotion Rules
Subscribe to my newsletter
Read articles from Lilavati Mhaske directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Lilavati Mhaske
Lilavati Mhaske
Hi, Iโm Lilavati Mhaske โ a tech enthusiast, Java learner, and aspiring full stack developer. I created this blog to document and share my journey through Full Stack Java development, from the fundamentals to frameworks like Spring Boot, REST APIs, databases, and frontend integration. I believe in learning by doingโand explaining! Whether you're just starting out or brushing up your Java skills, I hope my posts make your learning smoother and more enjoyable. Letโs grow together, one line of code at a time. โ๐ป