Java Language Essentials:

Lilavati MhaskeLilavati Mhaske
5 min read

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 type

  • a โ€“ Variable name (identifier)

  • = โ€“ Assignment operator

  • b, 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 literal

  • true โ€“ Boolean literal

  • "Java" โ€“ String literal


๐Ÿงฎ Number Systems in Java

SystemPrefixValid ExampleExplanation
Binary0b/0Bint x = 0b1010;Represents binary 1010 โ†’ decimal 10
Octal0int x = 0754;Represents octal 754 โ†’ decimal 492
Decimal(none)int x = 100;Regular base-10 number
Hexadecimal0x/0Xint 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, and final 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ยฒ = 4

  • a >> 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

ConceptWhy It Matters
TokensBreaks source code into meaningful units
IdentifiersAllows naming variables, classes, methods
LiteralsRepresents constant data directly in code
Number SystemsSupports multiple numeric formats (binary, octal, etc.)
KeywordsReserved for language syntax
OperatorsCore 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


1
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. โ˜•๐Ÿ’ป