In Java, keywords are reserved words that have a specific meaning to the compiler. These words cannot be used as identifiers (like variable names, class names, or method names). They define the structure and flow of a Java program.
There are 50+ Java keywords (as of Java 17), each playing a unique role. Let’s walk through each keyword with explanations and examples where applicable.
Java Keywords Table
| Keyword | Description |
|---|---|
| abstract | Defines an abstract class or method without implementation. |
| assert | Used for debugging; tests assumptions in code. |
| boolean | Defines a variable with true or false value. |
| break | Exits the current loop or switch block. |
| byte | Defines a variable of 8-bit signed integer. |
| case | Defines individual conditions in a switch statement. |
| catch | Handles exceptions thrown in the try block. |
| char | Declares a single 16-bit Unicode character. |
| class | Declares a class in Java. |
| const | Reserved but not used in Java. |
| continue | Skips current iteration in loop and continues with the next. |
| default | Specifies default block in switch or method in interface. |
| do | Starts a do-while loop. |
| double | Declares a 64-bit floating-point number. |
| else | Specifies the block to run if condition in if is false. |
| enum | Defines a fixed set of constants. |
| extends | Indicates a subclass or interface extension. |
| final | Used to declare constants, prevent method override or class inheritance. |
| finally | Block that always executes after try-catch. |
| float | Declares a 32-bit floating-point number. |
| for | Used to start a for loop. |
| goto | Reserved but not used. |
| if | Used to execute block based on condition. |
| implements | Used when a class implements an interface. |
| import | Includes other classes or packages. |
| instanceof | Tests whether an object is an instance of a specific class or interface. |
| int | Declares a 32-bit signed integer. |
| interface | Declares a contract with method signatures only. |
| long | Declares a 64-bit signed integer. |
| native | Declares a method implemented in native (non-Java) code. |
| new | Creates new objects. |
| null | Represents no value or reference. |
| package | Specifies the package for the class. |
| private | Restricts access to within the same class. |
| protected | Access limited to subclass or same package. |
| public | Accessible from anywhere. |
| return | Exits from a method and optionally returns a value. |
| short | Declares a 16-bit signed integer. |
| static | Used for class-level variables and methods. |
| strictfp | Ensures floating point calculations are consistent across platforms. |
| super | Refers to parent class constructor or methods. |
| switch | Executes block based on matching case. |
| synchronized | Controls thread access to blocks/methods. |
| this | Refers to current class instance. |
| throw | Throws an exception explicitly. |
| throws | Declares exceptions a method might throw. |
| transient | Prevents serialization of a variable. |
| try | Defines a block to test for exceptions. |
| void | Specifies no return type for a method. |
| volatile | Marks variable as changed by different threads. |
| while | Starts a while loop based on condition. |
Example: Using Some Common Java Keywords
public class Demo {
public static void main(String[] args) {
int count = 5;
for (int i = 0; i < count; i++) {
if (i == 3) {
continue; // skip 3
}
System.out.println("i = " + i);
}
}
}
i = 0
i = 1
i = 2
i = 4
Another Example: Try-Catch with Exception Handling
public class ExceptionDemo {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
} finally {
System.out.println("Finally block always runs.");
}
}
}
Cannot divide by zero!
Finally block always runs.