What is a keyword in Java?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

A keyword in Java is a reserved word that has a specific meaning and purpose within the language. Keywords cannot be used as identifiers (like variable names, method names, or class names) because they are part of Java's syntax. Each keyword has a predefined function and plays a vital role in defining the structure and behavior of the Java program.

Examples of Common Java Keywords:

  1. class: Defines a new class.
    public class Example { }
  2. public: Specifies the access level for classes, methods, and variables (accessible from anywhere).
    public void method() { }
  3. static: Indicates that a method or variable belongs to the class rather than instances of the class.
    public static void main(String[] args) { }
  4. void: Specifies that a method does not return any value.
    public void display() { }
  5. if: Used to create conditional statements.
    if (a > b) { }

Java has 52 Reserved Keywords, including:

  • Control flow statements: if, else, for, while, switch, case
  • Access modifiers: public, private, protected
  • Data types: int, char, boolean, float, etc.
  • Exception handling: try, catch, finally, throw, throws
  • OOP keywords: class, extends, implements, this, super

Special Keywords:

  • null, true, and false are not technically keywords but are reserved literals in Java.

Reference Sources:

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is Event-Driven Architecture vs Request-Driven Architecture?
How to answer what are your weaknesses in a pm interview?
What are the 5 essentials of cloud computing?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.