Loading
Keywords in Java are a set of predefined, reserved words that form the core vocabulary of the language. These words are integral to the structure and syntax of Java, each serving a unique and essential purpose in how programs are written and executed. Java keywords represent specific commands or instructions to the compiler, such as defining classes, controlling program flow, declaring data types, or handling exceptions.

Because each keyword has a fixed meaning within the Java language, they are strictly reserved and cannot be repurposed or used as identifiers. This means you cannot use any keyword as the name for your variables, methods, classes, interfaces, or any other user-defined elements in your code. Attempting to do so will result in a compilation error, as the compiler would not be able to distinguish between your intended use and the keyword’s built-in function.


Key Point

  • Keywords are case-sensitive and must be used exactly as defined (e.g., while is a keyword, but While is not).
  • They form the foundation of Java’s syntax and control program structure, flow, and features.
  • You cannot use keywords as identifiers (variable, class, or method names).


Complete List of Java Keywords

Java currently has 50+ reserved keywords (the exact number can vary slightly by Java version and context). Here are some of the most important ones, grouped by category:

KeywordDescription
abstractDeclares abstract classes or methods
assertUsed for debugging
booleanDeclares a boolean variable
breakExits a loop or switch
byteDeclares a byte variable
caseDefines a case in a switch statement
catchHandles exceptions
charDeclares a character variable
classDeclares a class
continueSkips to the next iteration of a loop
defaultDefault case in switch
doStarts a do-while loop
doubleDeclares a double variable
elseAlternative block for if
enumDeclares an enumeration
extendsIndicates inheritance
finalDefines constants or prevents overriding/inheritance
finallyExecutes after try-catch
floatDeclares a float variable
forStarts a for loop
ifStarts a conditional block
implementsIndicates interface implementation
importImports classes/packages
instanceofChecks object type
intDeclares an integer variable
interfaceDeclares an interface
longDeclares a long variable
nativeIndicates native method
newCreates new objects
packageDeclares a package
privatePrivate access modifier
protectedProtected access modifier
publicPublic access modifier
returnReturns from a method
shortDeclares a short variable
staticDeclares a static member
strictfpEnsures floating-point consistency
superRefers to superclass
switchStarts a switch statement
synchronizedEnsures thread safety
thisRefers to current object
throwThrows an exception
throwsDeclares exceptions thrown
transientSkips field during serialization
tryStarts a try block
voidIndicates no return value
volatileMarks variable for thread safety
whileStarts a while loop


Special reserved literals:

true, false, null (these are not technically keywords, but are reserved for literal values).


Contextual Keywords

Java also has contextual (restricted) keywords introduced in newer versions (Java 9+). These words act as keywords only in certain contexts, allowing them to be used as identifiers elsewhere. Examples include:

  • exports, module, open, opens, permits, provides, record, requires, sealed, to, transitive, uses, var, with, yield, non-sealed


Note:  Some contextual keywords (like record, sealed, var, yield, permits) cannot be used as class names or type identifiers in recent Java versions.


Unused Reserved Keywords

Some words are reserved but not currently used in Java. These include:

  • const (use final instead)
  • goto (not supported in Java, as it leads to poor code structure)
  • strictfp (used for floating-point consistency, but rarely needed)

What Are Identifiers in Java?

Identifiers are names you assign to variables, methods, classes, packages, and interfaces. They help you reference and organize your code.



Key Point

  • Identifiers are user-defined names for program elements.
  • They must start with a letter (A-Z, a-z), underscore (_), or dollar sign ($).
  • Subsequent characters can be letters, digits (0-9), underscores, or dollar signs.
  • Identifiers are case-sensitive (MyVar and myvar are different).
  • No spaces or special characters (like @, #, %) are allowed.
  • Cannot be a Java keyword or reserved word.
  • There is no length limit, but concise, meaningful names are recommended.


Example: Valid Identifiers

int age;
String firstName;
double _salary;
float $rate;


Example: Invalid Identifiers

int 1stValue;      // Cannot start with a digit
float my-rate;     // Hyphens not allowed
String class;      // 'class' is a keyword
int my value;      // Spaces not allowed


Best Practices for Identifiers

  • Use descriptive names (e.g., employeeCount instead of ec).
  • Follow camelCase for variables and methods (totalAmount, calculateSum).
  • Use PascalCase for class names (EmployeeDetails).
  • Avoid starting identifiers with $ or _ unless required.
  • Do not use Java keywords as identifiers.


Special Note: const and goto in Java

  • const and goto are reserved but not used in Java.
  • Use final for constants instead of const.
  • Java does not support goto; use structured control statements like if, for, and while instead.


Example: Keyword Vs. Identifiers

public class Addition {           // 'class' is a keyword, 'Addition' is an identifier
    public static void main(String[] args) { // 'public', 'static', 'void' are keywords; 'main', 'args' are identifiers
        int a = 10;               // 'int' is a keyword, 'a' is an identifier
        int b = 20;               // 'int' is a keyword, 'b' is an identifier
        int sum = a + b;          // 'sum' is an identifier
        System.out.println(sum);  // 'System', 'out', 'println' are identifiers (from Java library)
    }
}


Understanding the difference between keywords and identifiers is fundamental in Java.

  • Keywords are reserved words that define the structure and rules of the language-they cannot be used for naming.
  • Identifiers are names you create for your program’s elements-follow the rules to ensure your code is readable and error-free.