Skip to main content

Command Palette

Search for a command to run...

Java Language Essentials:

Published
5 min readView as Markdown
Java Language Essentials:
L

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. ☕💻


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


G

Perfect explanation.

More from this blog

Full Stack Java Journey

5 posts

Exploring Java’s journey from its origins to modern Full Stack development. Follow along as I share insights, tutorials, and lessons from my Java learning experience. ☕💻