BCA / B.Tech 11 min read

Keywords in Java

What are Keywords in Java?

Keywords are predefined, reserved words in the Java language that have a special meaning to the compiler. They are the building blocks of the language's syntax. Because they are reserved, you cannot use them as names for your variables, methods, classes, or any other identifiers.

Key Characteristics:
  • Reserved: They are part of the Java syntax and cannot be used as identifiers. Trying to do so will result in a compilation error.
  • Lowercase: All Java keywords are written in lowercase.
  • Special Meaning: Each keyword has a specific function that the Java compiler understands.

Example of Misuse:

// This code will not compile because `int` is a keyword.
int int = 10; // Compile-time error: ' expected'
        

Common Categories of Keywords:
  • Data Types: byte, short, int, long, float, double, char, boolean, void.
  • Control Flow: if, else, switch, case, default, for, while, do-while, break, continue, return.
  • Class and Object Related: class, interface, enum, extends, implements, new, this, super, instanceof.
  • Access & Other Modifiers: public, private, protected, static, final, abstract, synchronized.
  • Exception Handling: try, catch, finally, throw, throws.

Note: While true, false, and null seem like keywords, they are technically considered literals. Similarly, const and goto are reserved words but are not used in the language.