![]() |
VOOZH | about |
Java keywords are reserved words with predefined meanings used by the compiler to perform specific operations. They are part of the language syntax and cannot be modified.
Successful demonstration of keywords.
The following diagram represents the classification of reserved words available in Java 21, including keywords, unused keywords, and reserved literals.
Using a keyword as a variable name would give error as shown below.
./Geeks.java:5: error: not a statement
String this = "Hello World!";
^
./Geeks.java:5: error: ';' expected
String this = "Hello World!";
2 errors
Important Points:
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 52 keywords, categorized below by their usage and purpose.
Used to define variable types and specify the kind of data they can hold.
| Keyword | Usage |
|---|---|
| boolean | Defines a variable that holds true or false. |
| byte | Defines an 8-bit signed integer. |
| char | Defines a 16-bit Unicode character. |
| short | Defines a 16-bit signed integer. |
| int | Defines a 32-bit signed integer. |
| long | Defines a 64-bit signed integer. |
| float | Defines a 32-bit floating-point number. |
| double | Defines a 64-bit floating-point number. |
| void | Specifies that a method does not return any value. |
Used to control the execution flow of a program, including loops, branching, and jumps.
| Keyword | Usage |
|---|---|
| if | Executes code when a condition is true. |
| else | Defines an alternate block when an if condition is false. |
| switch | Selects one block of code among multiple options. |
| case | Defines an individual branch in a switch statement. |
| default | Defines the block executed if no case matches. |
| for | Starts a for loop. |
| while | Starts a while loop. |
| do | Starts a do-while loop. |
| break | Terminates the nearest loop or switch. |
| continue | Skips to the next iteration of a loop. |
| return | Exits from a method and optionally returns a value. |
Used for handling and managing runtime errors in programs.
| Keyword | Usage |
|---|---|
| try | Defines a block of code to test for exceptions. |
| catch | Defines a block to handle exceptions thrown by try. |
| finally | Defines a block that always executes after try and catch. |
| throw | Used to manually throw an exception. |
| throws | Declares the exceptions a method can throw. |
| assert | Tests assumptions during program execution for debugging. |
Used to define classes, interfaces, and objects, as well as inheritance and encapsulation properties.
| Keyword | Usage |
|---|---|
| class | Declares a class. |
| interface | Declares an interface. |
| extends | Indicates inheritance from a superclass. |
| implements | Indicates that a class implements an interface. |
| new | Creates new objects. |
| this | Refers to the current object. |
| super | Refers to the superclass of the current object. |
| abstract | Declares a class or method that must be implemented in a subclass. |
| final | Prevents inheritance, overriding, or modification. |
| static | Declares class-level variables or methods. |
| sealed | Restricts which classes can extend a given class. |
| permits | Specifies the subclasses allowed to extend a sealed class. |
enum | Declares a fixed set of constants. |
record | Declares an immutable data class (Java 16+). |
instanceof | Checks whether an object is of a specific type. |
Define the visibility or accessibility of classes, methods, and variables.
| Keyword | Usage |
|---|---|
| public | Accessible from anywhere in the program. |
| protected | Accessible within the same package or by subclasses. |
| private | Accessible only within the same class. |
Used to organize classes and access external code.
| Keyword | Usage |
|---|---|
| package | Defines a namespace for classes. |
| import | Allows access to classes from other packages. |
Used to handle concurrent execution of code and ensure thread safety.
| Keyword | Usage |
|---|---|
| synchronized | Defines critical sections that only one thread can access at a time. |
| volatile | Indicates that a variable may change asynchronously. |
Handle object persistence, garbage collection, and native method calls.
| Keyword | Usage |
|---|---|
| transient | Excludes a variable from serialization. |
| native | Specifies that a method is implemented in native (non-Java) code. |
Define additional behaviors and precision control.
| Keyword | Usage |
|---|---|
| strictfp | Ensures consistent floating-point calculations across platforms. |
These keywords are reserved but not currently used by Java.
| Keyword | Usage |
|---|---|
| const | Reserved for future use; not currently implemented. |
| goto | Reserved for future use; not currently implemented. |
Special literals represent predefined constant values in Java and cannot be used as identifiers.
| Keyword | Usage |
|---|---|
| true | Represents the boolean value true. |
| false | Represents the boolean value false. |
| null | Represents the absence of any reference value. |
| Feature | Keywords | Identifiers |
|---|---|---|
| Definition | Reserved words with predefined meanings in Java | User-defined names used for variables, methods, classes, etc. |
| Purpose | Define the syntax and structure of Java programs | Identify program elements |
| Can be Modified | No | Yes |
| Usage as Variable Name | Not allowed | Allowed |
| Predefined by Java | Yes | No |
| Examples | int, class, if, return | studentName, calculateSum, Employee |
| Case Sensitive | Yes | Yes |
| Compiler Meaning | Fixed meaning | Meaning defined by programmer |