Open In App

Java Keywords

Last Updated : 04 Dec, 2025
Comments
Improve
Suggest changes
113 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
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:5: error: not a statement

String this = "Hello World!";

^

./Geeks.java:5: 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
trueRepresents the boolean value true.
falseRepresents the boolean value false.
nullRepresents the absence of any reference value.
Suggested Quiz
5 Questions

Which of the following is NOT a Java keyword?

  • A

    volatile

  • B

    goto

  • C

    const

  • D

    true

Explanation:

true, false, and null look like keywords but are actually boolean and null literals, not Java keywords. They cannot be used as identifiers but are not part of Java's keyword list.

What will happen if you use a Java keyword as a variable name?

  • A

    The program runs normally

  • B

    The compiler automatically renames it

  • C

    Compilation error occurs

  • D

    It becomes a string value

Explanation:

Java keywords are reserved words and cannot be used as variable names or identifiers. The compiler throws an error like: error: not a statement or ';' expected.

Which keyword is used to manually throw an exception?

  • A

    throws

  • B

    catch

  • C

    throw

  • D

    finally

Explanation:

throw is used inside methods to manually create and throw an exception object, e.g.,

throw new RuntimeException("Error");

Which keyword ensures only one thread accesses a block at a time?

  • A

    synchronized

  • B

    volatile

  • C

    transient

  • D

    static

Explanation:

synchronized is used in multithreading to lock a method/block so only one thread can execute it at a time, preventing thread interference.

Which category does the keyword extends belong to?

  • A

    Control Flow

  • B

    Exception Handling

  • C

    Object-Oriented

  • D

    Access Control

Explanation:


Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Article Tags :

Explore