Keywords

Java has 50+ reserved keywords (as of Java 17). These are reserved by the language and cannot be used as identifiers (variable names, class names, etc.).

We’ll break them into logical groups and explain purpose, usage, and nuances of each with code examples.


πŸ“¦ 1. Data Types & Literals

These define the type of data.

KeywordMeaning
int32-bit integer
long64-bit integer
short16-bit integer
byte8-bit integer
float32-bit floating point
double64-bit floating point
char16-bit Unicode character
booleantrue or false
true, false, nullliterals

πŸ”Έ null is not a keyword but a reserved literal in Java.


🧱 2. Access Modifiers

These control visibility.

KeywordPurpose
publicVisible everywhere
privateVisible only in the same class
protectedVisible in the same package + subclasses
default(No keyword) Package-private

KeywordPurpose
classDefines a class
interfaceDeclares an interface
enumDeclares an enumeration
extendsInherits from a class
implementsImplements an interface
newCreates an object
thisRefers to current object
superRefers to parent class

πŸ” 4. Control Flow

KeywordPurpose
if / elseBranching
switch, case, defaultMulti-way branching
while, do, forLoops
break, continueControl loop execution
returnExit method
yieldReturn value from a switch expression (Java 14+)

βš™οΈ 5. Exception Handling

KeywordPurpose
tryStart of exception block
catchHandle the exception
finallyAlways executes
throwUsed to throw exception
throwsDeclares possible exceptions

πŸ” 6. Object Behavior Modifiers

KeywordPurpose
staticBelongs to the class, not instance
finalCannot be changed/overridden
abstractDeclares incomplete method/class
synchronizedUsed in multithreading to control access
volatilePrevents thread caching for variables
transientSkips variable during serialization
nativeCalls code written in another language (C/C++)
strictfpEnsures consistent floating-point calculations across platforms

🧬 7. Inheritance and Polymorphism

KeywordPurpose
extendsSubclassing (class inheritance)
implementsInterface implementation
instanceofChecks runtime type
superAccess parent class methods/constructors

πŸ‘¨β€πŸ‘©β€πŸ‘§ 8. Concurrency (Threads)

KeywordPurpose
synchronizedEnsures atomic access to code block
volatileGuarantees visibility of changes across threads

πŸ“¦ 9. Package and Import

KeywordPurpose
packageDeclares package name
importImports other packages/classes

πŸ“ 10. Miscellaneous

KeywordPurpose
assertDebugging tool for assumptions
instanceofCheck if object is of a specific type
gotoReserved but not used in Java
constReserved but not used (use final instead)

πŸ†• Bonus: New Keywords in Recent Java Versions

KeywordPurpose
varLocal variable type inference (Java 10+)
recordImmutable data class (Java 14+)
sealed, permits, non-sealedRestricted class hierarchy (Java 15+)