Java Reserved Words (Keywords & Literals)
Imagine you're talking to Java.
Just like we use words such as "if", "while", or "return" in English to express a meaning, Java also has its own special words.
These words already have a predefined meaning, so we cannot use them as variable names, class names, or method names.
These special words are called Reserved Words.
What are Reserved Words?
Reserved Words are words that are already reserved by Java to perform a specific task or represent a specific value.
For example,
int age = 20;
Here, int is a reserved word that tells Java the variable will store an integer value.
If you try to use a reserved word as a variable name,
int class = 10;
❌ Compile Time Error
Because class is already reserved by Java.
1. Keywords (50)
Keywords are reserved words that tell Java what action to perform.
Instead of memorizing all 50 keywords randomly, it's much easier to learn them category by category.
Used Keywords (48)
For Datatypes: (8)
byte, short, int, long, float, double, boolean, char
For flow control: (11)
if, else, switch, case, default, while, do, for, break, continue, return
For modifiers: (11)
public, private, protected, static, final, abstract, synchronized, native, strictfp(1.2v), transient, volatile
For exception handling: (6)
try, catch, finally, throw, throws, assert(1.4v)
For class related keywords: (6)
class, interface, extends, implements, package, import
Object related keywords: (4)
new, instanceof, super, this
Return type: (1)
void
For named constants: (1)
enum
Example: enum month { JAN, FEB, MAR.... DEC }
Unused Keywords (2)
goto - uasge of this keyword created several problems in old languages. so sun people banned this keyword
const - used to define a constant, but using final we can define that if we try to use this unused keywords in java program we will get compile time error.
2. Reserved Literals (3)
Unlike keywords, literals represent fixed values.
Java reserves three literals.
true, false - represents boolean valuenull - default value for object reference
Important Interview Point
One interesting fact about Java is:
Java provides the new keyword to create objects.
But Java does not have a delete keyword.
Why?
Because Java automatically removes unused objects using the Garbage Collector (GC).
So developers only create objects.
Java itself takes care of destroying objects that are no longer needed.
Create Object → new
Delete Object → Garbage Collector (Automatic)
Quick Interview Question
Which of the following contains only valid Java keywords?
A. new, deleteB. goto, constantC. break, continue, return, exitD. final, finally, finalizeE. throw, throws, thrownF. notify, notifyAllG. implements, extends, importsH. sizeof, instanceOfI. instanceOf, strictfpJ. byte, short, intK. None of the Above
Summary
- Java has 53 Reserved Words.
- They are divided into 50 Keywords and 3 Reserved Literals.
- Keywords define Java's functionality.
-
Reserved literals represent fixed values like
true,false, andnull. -
gotoandconstare reserved but never used. -
Java has a
newkeyword but nodeletekeyword because the Garbage Collector automatically removes unused objects.
.png)
Comments
Post a Comment