Open In App

Java Keywords

Last Updated : 11 Nov, 2025
Comments
Improve
Suggest changes
109 Likes
Like
Report

In Java, keywords are the reserved words that have some predefined meanings and are used by the Java compiler for some internal process or represent some predefined actions. These words cannot be used as identifiers such as variable names, method names, class names, or object names. 

Now, let us go through a simple example before a deep dive into the article.

Java
// Java Program to demonstrate Keywords
class Geeks {
    
    public static void main(String[] args)
    {
      	// Using final and int keyword
        final int x = 10;
      
      	// Using if and else keywords
      	if(x > 10){
           System.out.println("Failed");
        }
      	else {
           System.out.println("Successful demonstration"
                              +" of keywords.");
        }
    }
}

Output
Successful demonstration of keywords.

What Happens if we use a Variable Name same as keyword?

Using a keyword as a variable name would give error as shown below.

Java
class Geeks 
{
    public static void main(String[] args)
    {
        String this = "Hello World!";
        System.out.println(this);
    }
}

Output:

./Geeks.java:9: error: not a statement        String this = "Hello World!";        ^./Geeks.java:9: error: ';' expected        String this = "Hello World!";              ^2 errors

Important Points:

  • The keywords const and goto are reserved, even though they are not currently used in Java.
  • true, false, and null look like keywords, but in actuality they are literals. However, they still can't be used as identifiers in a program.
  • In Java, keywords are case-sensitive, and writing Java keywords in upper case (like IF instead of if) will throw an error.

Java Keywords List (As of Java 21)

Java keywords are reserved words that have predefined meanings in the language. They cannot be used as identifiers (like variable or method names).

As of Java 21, there are 53 keywords, categorized below by their usage and purpose.

1. Data Type Keywords

Used to define variable types and specify the kind of data they can hold.

KeywordUsage
booleanDefines a variable that holds true or false.
byteDefines an 8-bit signed integer.
charDefines a 16-bit Unicode character.
shortDefines a 16-bit signed integer.
intDefines a 32-bit signed integer.
longDefines a 64-bit signed integer.
floatDefines a 32-bit floating-point number.
doubleDefines a 64-bit floating-point number.
voidSpecifies that a method does not return any value.

2. Control Flow Keywords

Used to control the execution flow of a program, including loops, branching, and jumps.

KeywordUsage
ifExecutes code when a condition is true.
elseDefines an alternate block when an if condition is false.
switchSelects one block of code among multiple options.
caseDefines an individual branch in a switch statement.
defaultDefines the block executed if no case matches.
forStarts a for loop.
whileStarts a while loop.
doStarts a do-while loop.
breakTerminates the nearest loop or switch.
continueSkips to the next iteration of a loop.
returnExits from a method and optionally returns a value.

3. Exception Handling Keywords

Used for handling and managing runtime errors in programs.

KeywordUsage
tryDefines a block of code to test for exceptions.
catchDefines a block to handle exceptions thrown by try.
finallyDefines a block that always executes after try and catch.
throwUsed to manually throw an exception.
throwsDeclares the exceptions a method can throw.
assertTests assumptions during program execution for debugging.

4. Object-Oriented Keywords

Used to define classes, interfaces, and objects, as well as inheritance and encapsulation properties.

KeywordUsage
classDeclares a class.
interfaceDeclares an interface.
extendsIndicates inheritance from a superclass.
implementsIndicates that a class implements an interface.
newCreates new objects.
thisRefers to the current object.
superRefers to the superclass of the current object.
abstractDeclares a class or method that must be implemented in a subclass.
finalPrevents inheritance, overriding, or modification.
staticDeclares class-level variables or methods.
sealedRestricts which classes can extend a given class.
permitsSpecifies the subclasses allowed to extend a sealed class.

5. Access Control Keywords

Define the visibility or accessibility of classes, methods, and variables.

KeywordUsage
publicAccessible from anywhere in the program.
protectedAccessible within the same package or by subclasses.
privateAccessible only within the same class.

6. Package and Import Keywords

Used to organize classes and access external code.

KeywordUsage
packageDefines a namespace for classes.
importAllows access to classes from other packages.

7. Multithreading and Synchronization Keywords

Used to handle concurrent execution of code and ensure thread safety.

KeywordUsage
synchronizedDefines critical sections that only one thread can access at a time.
volatileIndicates that a variable may change asynchronously.

8. Memory Management and Object Serialization Keywords

Handle object persistence, garbage collection, and native method calls.

KeywordUsage
transientExcludes a variable from serialization.
nativeSpecifies that a method is implemented in native (non-Java) code.

9. Modifier and Utility Keywords

Define additional behaviors and precision control.

KeywordUsage
strictfpEnsures consistent floating-point calculations across platforms.

10. Reserved (Unused) Keywords

These keywords are reserved but not currently used by Java.

KeywordUsage
constReserved for future use; not currently implemented.
gotoReserved for future use; not currently implemented.

11. Special Literals

KeywordUsage
nullRepresents the absence of any reference value.

Article Tags :

Explore