☕ Java

Java Keywords

Java has 67 keywords — reserved words that the compiler treats as part of the language syntax. You can't use them as identifiers. Understanding what each keyword does and which category it belongs to gives you a mental map of the entire Java language.

What Are Keywords?

Keywords are words with predefined meaning in the Java language. The compiler recognizes them as part of its grammar — they can't be used as variable names, class names, method names, or any other identifier. Attempting to use a keyword as an identifier results in a compile error. Java currently has 67 keywords. Some are used constantly (class, int, if, return). Some are rarely seen but important to know (transient, volatile, strictfp). Two are reserved but not yet used (goto, const). Here they are by category.

Primitive Data Type Keywords

These eight keywords define Java's built-in primitive types — the basic data types that are not objects:
Java
byte    b = 127;           // 8-bit integer, range: -128 to 127
short   s = 32000;         // 16-bit integer
int     i = 2_000_000;     // 32-bit integer (most common)
long    l = 9_000_000_000L;// 64-bit integer (note the L suffix)
float   f = 3.14f;         // 32-bit floating point (note the f suffix)
double  d = 3.14159265;    // 64-bit floating point (default for decimals)
char    c = 'A';           // 16-bit Unicode character
boolean flag = true;       // true or false — nothing else

Control Flow Keywords

These keywords control the order of execution — decisions, loops, and jumps:
Java
// Conditionals:
if (score >= 90) { grade = "A"; }
else if (score >= 80) { grade = "B"; }
else { grade = "C"; }

switch (day) {
    case "Monday": System.out.println("Start of week"); break;
    default: System.out.println("Midweek");
}

// Loops:
for (int i = 0; i < 10; i++) { }           // counted loop
while (condition) { }                       // pre-condition loop
do { } while (condition);                  // post-condition loop (runs at least once)

// Loop control:
for (int i = 0; i < 10; i++) {
    if (i == 5) continue;  // skip this iteration
    if (i == 8) break;     // exit the loop entirely
}

// Method exit:
return value;   // return a value from a method
return;         // return from void method

Class and Object Keywords

These keywords define the structure of classes, objects, and their relationships:
Java
// Defining types:
class Animal { }
interface Flyable { }
enum Season { SPRING, SUMMER, AUTUMN, WINTER }
record Point(int x, int y) { }   // Java 16+

// Modifiers:
abstract class Shape { abstract void draw(); }  // can't be instantiated
final class ImmutableValue { }                  // can't be subclassed
final int MAX = 100;                            // can't be reassigned

// Inheritance:
class Dog extends Animal { }       // extends: inherit from a class
class Dog implements Flyable { }   // implements: fulfill an interface contract

// Object creation and reference:
Animal a = new Animal();    // new: allocate and initialize an object
Animal b = null;            // null: absence of an object reference

// Access within class hierarchy:
class Dog extends Animal {
    Dog() { super(); }               // super: call parent constructor
    void bark() { this.name = ""; }  // this: refers to current object
}

// Type checking:
if (a instanceof Dog) {          // instanceof: check runtime type
    Dog d = (Dog) a;             // cast
}

Access Modifier Keywords

These four keywords control visibility — what code can access what:
Java
public class Account {

    public String name;      // accessible from anywhere

    protected double balance; // accessible within package + subclasses

    // (no keyword) = package-private
    int transactionCount;    // accessible only within the same package

    private String password; // accessible only within this class
}

// Rule of thumb for fields: always private
// Rule of thumb for methods: public if it's the API, private if it's internal
// Rule of thumb for classes: public if used outside the package

Exception Handling Keywords

These keywords manage error handling and the exception propagation mechanism:
Java
// Try-catch-finally:
try {
    int result = 10 / 0;                    // might throw
} catch (ArithmeticException e) {           // catch specific exception
    System.out.println("Can't divide by zero: " + e.getMessage());
} catch (Exception e) {                     // catch broader exception
    System.out.println("Something went wrong");
} finally {
    System.out.println("Always runs — close resources here");
}

// Throwing exceptions:
void withdraw(double amount) {
    if (amount > balance) {
        throw new IllegalArgumentException("Insufficient funds");
    }
}

// Declaring checked exceptions:
void readFile(String path) throws IOException {
    // caller must handle or re-declare this exception
}

// Custom exception:
class InsufficientFundsException extends Exception {
    InsufficientFundsException(String message) { super(message); }
}

Other Important Keywords

The remaining keywords cover memory, threading, type operations, and module system:
Java
// Memory and threading:
static int instanceCount = 0;     // static: belongs to class, not instance
volatile boolean running = true;  // volatile: always read from main memory (threading)
synchronized void deposit() { }   // synchronized: only one thread at a time
transient String tempData;        // transient: skip this field during serialization

// Type operations:
void process(Object obj) {
    if (obj instanceof String s) {   // instanceof: type check (+ pattern match Java 16+)
        System.out.println(s.length());
    }
}

// Package and imports:
package com.example.app;           // declares this file's package
import java.util.List;             // imports a specific class
import java.util.*;                // imports all classes in package

// Module system (Java 9+):
// module-info.java:
module com.example.app {
    requires java.base;
    exports com.example.app.api;
}

// Rarely used but valid:
strictfp double calculate() { }  // strictfp: IEEE 754 strict floating point
native void nativeMethod();      // native: implemented in C/C++ via JNI
assert x > 0 : "x must be positive";  // assert: debug-time condition check

Reserved But Unused: goto and const

Java reserves two keywords that have no function in the language — goto and const. They can't be used as identifiers, but they also don't do anything. goto was reserved to prevent developers from writing unstructured jump logic (a common source of bugs in C). Java provides structured alternatives — break with labels, continue, and return — that cover every legitimate use case. const was reserved as the intended keyword for declaring constants, but Java went with the final keyword instead. The word const is reserved to prevent confusion for developers coming from C/C++ where const has a specific meaning.
Java
// These are reserved keywords — you cannot use them as identifiers:
int goto = 5;    // compile error: illegal start of expression
int const = 10;  // compile error: illegal start of expression

// Use these instead:
// goto → use break with labels:
outer:
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        if (j == 3) break outer;  // breaks out of both loops
    }
}

// const → use final:
final int MAX = 100;