Reserved words in java

Reserved Words in Java

Introduction

In Java, some words are already reserved to perform specific tasks.
These words have special meaning in the Java language and cannot be used as identifiers such as variable names, method names, or class names.

Such predefined words are called reserved words in Java.

What Are Reserved Words in Java?

Reserved words are predefined keywords that are used by the Java compiler to understand the structure and logic of a program.

👉 If we try to use any reserved word as an identifier, the compiler will generate a compile-time error.

Categories of Reserved Words in Java

Java reserved words are grouped based on their purpose.

Reserved Words for Data Types

These keywords are used to define primitive data types.

  • byte

  • short

  • int

  • long

  • float

  • double

  • char

  • boolean

Reserved Words for Flow Control

These keywords control the execution flow of a program.

  • if

  • else

  • switch

  • case

  • default

  • for

  • do

  • while

  • break

  • continue

  • return

Keywords Used as Modifiers

These keywords define access level and behavior of variables, methods, or classes.

  • public

  • private

  • protected

  • static

  • final

  • abstract

  • synchronized

  • native

  • strictfp (introduced in Java 1.2)

  • transient

  • volatile

Keywords for Exception Handling

These keywords help handle runtime errors.

  • try

  • catch

  • finally

  • throw

  • throws

  • assert (introduced in Java 1.4)

Class-Related Keywords

These keywords are used while working with classes and packages.

  • class

  • package

  • import

  • extends

  • implements

  • interface

Object-Related Keywords

These keywords are related to object creation and reference.

  • new

  • instanceof

  • super

  • this

Void Return Type Keyword

If a method does not return any value, it must use the void keyword in Java.

  • void

👉 In Java, using void is mandatory, unlike C++ where it can be optional.

Unused Keywords in Java

Some keywords are reserved but not used in Java.

  • goto
    👉 Banned in Java because it creates complex and confusing code.

  • const
    👉 Java uses final instead of const.

⚠️ Using these keywords in a program will cause a compile-time error.

Reserved Literals in Java

These are fixed values and not keywords.

  • true – value for boolean type

  • false – value for boolean type

  • null – default value for object references

Enum Keyword

The enum keyword was introduced in Java 1.5.
It is used to define a group of named constants.

Example

enum Beer {
KF, RC, KO, FO;
}
 
Reserved Words in java

Important Notes

  • All Java reserved words are written in lowercase

  • Java is case-sensitive

  • String, Integer, and main are not keywords

  • finalize() is a method, not a keyword

Which of the following are valid Java keywords?

WordValid
public
static
void
main
String
args

Conclusion

Reserved words are the core building blocks of the Java language. They help the compiler understand how the program should behave. Since these words have predefined meanings, they cannot be used as identifiers. Learning reserved words is essential for writing error-free Java programs.

Keep learning Java with EasyHaiCode 🚀

Java Quiz : Reserved words in java

Quiz related to reserved word in java

1 / 7

What are reserved words in Java?

2 / 7

Which of the following cannot be used as an identifier in Java?

3 / 7

Which keyword is used to handle exceptions in Java?

4 / 7

Which keyword is used to create an object in Java?

5 / 7

Which of the following is an unused keyword in Java?

6 / 7

Which keyword was introduced in Java 1.5?

7 / 7

Which of the following are valid Java keywords?

Your score is

The average score is 0%

0%

0

Subtotal