Java Keywords


Keywords in Java are predefined, reserved words that have a special meaning to the Java compiler. These keywords cannot be used as identifiers (e.g., variable names, class names, or method names) because they serve specific roles in the language.

Java has a set of keywords that represent various functionalities such as data types, control structures, modifiers, and error handling. These keywords are case-sensitive and must be used exactly as written.

Categories of Java Keywords

  1. Data Types Keywords: These keywords are used to define various data types in Java.

    • byte: 8-bit integer
    • short: 16-bit integer
    • int: 32-bit integer
    • long: 64-bit integer
    • float: 32-bit floating point
    • double: 64-bit floating point
    • char: Single 16-bit Unicode character
    • boolean: True or false values
    • void: Used to specify that a method does not return a value
  2. Control Flow Keywords: These keywords control the flow of execution in a Java program.

    • if: Executes a block of code if a specified condition is true.
    • else: Executes a block of code if the condition in the if statement is false.
    • switch: Used to execute one block of code among many based on the value of an expression.
    • case: Defines a block of code to be executed in the switch statement.
    • default: Defines the block of code to be executed if no case matches in the switch.
    • for: Used for loops, iterating a block of code a specified number of times.
    • while: Executes a block of code as long as the condition is true.
    • do: Executes a block of code once and then repeats while the condition is true.
    • break: Exits a loop or switch statement prematurely.
    • continue: Skips the current iteration of a loop and moves to the next iteration.
    • return: Exits a method and optionally returns a value.
    • try: Defines a block of code to be tested for exceptions.
    • catch: Defines a block of code to handle exceptions.
    • finally: Defines a block of code that is always executed after the try/catch, whether an exception is handled or not.
    • throw: Used to explicitly throw an exception.
    • throws: Declares an exception that a method can throw.
  3. Modifiers Keywords: These keywords are used to define the scope and behavior of classes, methods, and variables.

    • public: The member is accessible from anywhere in the program.
    • private: The member is accessible only within the class.
    • protected: The member is accessible within the class, subclasses, and same package.
    • static: Indicates that a member belongs to the class rather than to instances of the class.
    • final: Prevents a class from being subclassed, a method from being overridden, or a variable from being reassigned.
    • abstract: Used to declare abstract classes and methods that must be implemented by subclasses.
    • synchronized: Used to synchronize a block of code or method for thread safety.
    • volatile: Indicates that a variable may be changed unexpectedly by other threads.
    • transient: Prevents a variable from being serialized during object serialization.
    • native: Specifies that a method is implemented in native code (usually in C or C++).
    • strictfp: Used to restrict floating-point calculations to ensure portability.
  4. Class-related Keywords: These keywords define classes and control their behavior.

    • class: Used to define a class.
    • extends: Specifies that a class inherits from a superclass.
    • implements: Specifies that a class implements an interface.
    • interface: Used to define an interface.
    • this: Refers to the current instance of the class.
    • super: Refers to the superclass of the current object.
    • new: Creates new objects.
    • instanceof: Tests if an object is an instance of a specific class or interface.
    • enum: Defines a set of named constants (an enumeration).
  5. Error Handling Keywords:

    • try, catch, finally, throw, throws: These keywords are used in exception handling, as explained earlier.
  6. Package-related Keywords: These keywords are used for organizing classes and controlling access.

    • package: Specifies the package to which the class belongs.
    • import: Allows the inclusion of other classes and packages in the current file.
  7. Object-related Keywords:

    • new: Used to create new objects.
    • this: Refers to the current instance of the class.
    • super: Refers to the parent class and is used to access members of the superclass.
  8. Others:

    • null: A literal value representing the absence of an object.
    • true and false: Literal values for the boolean data type.
    • assert: Used to test assumptions in the code (introduced in Java 1.4).
    • goto: Reserved but not used.
    • const: Reserved but not used.

Example Program with Keywords

public class Example { public static void main(String[] args) { int number = 10; // 'int' is a keyword if (number > 0) { // 'if' and 'else' are keywords System.out.println("Positive number"); } else { System.out.println("Non-positive number"); } } }

Summary of Java Keywords

KeywordDescription
abstractDeclares an abstract class or method
assertUsed for debugging purposes
booleanDeclares a boolean data type
breakExits a loop or switch
byteDeclares a byte data type
caseDefines cases in a switch statement
catchCatches exceptions
charDeclares a character data type
classDeclares a class
constNot used in Java (reserved)
continueSkips the current iteration of a loop
defaultSpecifies default case in switch statement
doUsed in do-while loops
doubleDeclares a double data type
elseSpecifies alternative path in if-else statement
enumDefines an enumeration
extendsSpecifies a superclass
finalDeclares constants, prevents overriding and inheritance
finallyExecutes code after try-catch block
floatDeclares a floating-point data type
forUsed for loops
gotoNot used in Java (reserved)
ifSpecifies a conditional statement
implementsSpecifies that a class implements an interface
importImports other classes or packages
instanceofTests if an object is an instance of a class or interface
intDeclares an integer data type
interfaceDeclares an interface
longDeclares a long integer data type
nativeSpecifies that a method is implemented in native code
newCreates new objects
nullRepresents a null reference
packageSpecifies a package
privateDeclares private access modifier
protectedDeclares protected access modifier
publicDeclares public access modifier
returnExits a method and optionally returns a value
shortDeclares a short integer data type
staticDeclares static fields or methods
strictfpRestricts floating-point calculations for portability
superRefers to superclass
switchSpecifies a switch statement
synchronizedUsed for thread synchronization
thisRefers to the current object
throwThrows an exception
throwsDeclares exceptions thrown by a method
transientPrevents serialization of fields
tryTries a block of code for exceptions
voidSpecifies no return type
volatileDeclares a volatile field (used in multithreading)
whileSpecifies a while loop

Conclusion

Java keywords form the foundation of the language. They are reserved words that perform specific functions and help define the structure and behavior of Java programs. Understanding how these keywords work is essential to mastering Java programming.