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 value

    null - 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, delete

B. goto, constant

C. break, continue, return, exit

D. final, finally, finalize

E. throw, throws, thrown

F. notify, notifyAll

G. implements, extends, imports

H. sizeof, instanceOf

I. instanceOf, strictfp

J. byte, short, int

K. 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, and null.
  • goto and const are reserved but never used.
  • Java has a new keyword but no delete keyword because the Garbage Collector automatically removes unused objects.

Comments

Popular posts from this blog

Java Roadmap

Java OOPs Concepts Made Simple: A Complete Beginner-Friendly Guide (Part-2)

Identifiers