Java Keywords
Java keywords are reserved words with a predefined meaning in the Java language. They form the foundation of Java syntax and cannot be used as identifiers (class names, method names, variable names).
As of modern Java versions, there are 50+ reserved keywords, plus a few reserved literals.
What Are Java Keywords?
- Predefined words understood by the Java compiler
- Used to define data types, control flow, access control, OOP behavior, and exception handling
- Case-sensitive (all are lowercase)
Why it matters: Incorrect use of keywords leads to compilation errors.
Categories of Java Keywords (Conceptual Grouping)
1. Access Modifiers
Control the visibility of classes, methods, and variables.
- public
- protected
- private
- default (no keyword)
Why it matters: Enforces encapsulation and security.
Example:
public class Test {
private int id;
}
2. Class, Object & OOP Keywords
Used to define class structure and behavior.
- class
- interface
- extends
- implements
- abstract
- new
- this
- super
Why it matters: Core to object-oriented programming.
Example:
class Car extends Vehicle {
Car() {
super();
}
}
3. Data Type Keywords
Used to declare primitive data types.
- byte
- short
- int
- long
- float
- double
- char
- boolean
Why it matters: Defines memory allocation and data handling.
4. Control Flow Keywords
Used to control program execution flow.
- if
- else
- switch
- case
- default
- for
- while
- do
- break
- continue
- return
Why it matters: Implements decision-making and looping logic.
5. Exception Handling Keywords
Used to handle runtime errors gracefully.
- try
- catch
- finally
- throw
- throws
Why it matters: Prevents application crashes and improves reliability.
6. Modifiers & Non-Access Keywords
Used to modify behavior of classes, methods, and variables.
- static
- final
- synchronized
- volatile
- transient
- native
- strictfp
Why it matters: Controls memory, threading, and performance.
7. Package & Import Keywords
Used for code organization and reuse.
- package
- import
Why it matters: Helps manage large applications and avoid naming conflicts.
8. Multithreading Keywords
Used in concurrent programming.
- synchronized
- volatile
Why it matters: Ensures thread safety.
9. Reserved Keywords (Rarely Used / Advanced)
- assert
- enum
- instanceof
Example:
if (obj instanceof String) {
// type check
}
Reserved Literals (Not Keywords)
These are not keywords, but have special meaning.
- true
- false
- null
Important Note: They cannot be used as identifiers.
Keywords That Are Reserved but Not Used
- goto
- const
Why they exist: Reserved for future use (to avoid conflicts).
Summary Table
| Category | Examples | Purpose |
|---|---|---|
| Access Modifiers | public, private | Control visibility |
| OOP | class, extends | Object-oriented structure |
| Data Types | int, boolean | Declare variables |
| Control Flow | if, for, switch | Execution logic |
| Exceptions | try, catch | Error handling |
| Modifiers | static, final | Behavior control |
| Packages | package, import | Code organization |
Common Mistakes by Beginners
- Using keywords as variable names
- Confusing this and super
- Misusing static
- Forgetting return
- Assuming default is an access modifier keyword
Interview-Ready Answers
Short Answer
Java keywords are reserved words that have predefined meanings and cannot be used as identifiers.
Detailed Answer
Java keywords define the structure and behavior of a Java program. They are used for declaring data types, controlling program flow, implementing OOP concepts, handling exceptions, and managing memory and threads.
Key Takeaway
Java keywords form the grammar of the Java language. Mastering them is essential for writing correct, readable, and maintainable Java code.