Table of Contents

    Keywords


    In programming languages, keywords are special reserved words that have a fixed meaning and cannot be used as variable names, function names, or identifiers.

    These words are part of the language syntax and are understood by the compiler/interpreter.


    Simple Definition

    Keywords are predefined, reserved words in a programming language that perform specific tasks and cannot be changed or reused for other purposes.


    Programming Fundamentals

    Keywords

    Learn what keywords are in programming, why they are reserved, how they are different from identifiers, and how Java keywords help define program structure, data types, conditions, loops, classes, and methods.

    What are Keywords in Programming?

    Keywords are special words in a programming language that have predefined meanings. These words are already reserved by the language, so programmers cannot use them as variable names, method names, class names, or other identifiers.

    In simple words, keywords are words that the programming language already understands. They are part of the language grammar and are used to write meaningful instructions.

    Keywords are reserved words that have a fixed meaning in a programming language.

    For example, in Java, words like class, public, static, void, int, if, else, for, and return are keywords.

    Easy Real-Life Example

    Keywords as Traffic Signals

    Think of keywords like traffic signals. A red light, green light, and yellow light have fixed meanings. Drivers cannot change their meaning. Similarly, programming keywords have fixed meanings, and programmers must use them according to language rules.

    If a programmer tries to use a keyword for another purpose, the compiler becomes confused and shows an error.

    Why are Keywords Important?

    Keywords are important because they give structure and meaning to a program. They help the compiler understand what the programmer wants to do.

    Importance of Keywords

    • Keywords define the structure of a program.
    • They help declare classes, methods, and variables.
    • They help define data types.
    • They help create conditions and loops.
    • They help control program flow.
    • They help define access levels such as public and private.
    • They make programming language syntax clear and consistent.
    • They prevent confusion by reserving important words for fixed purposes.

    Example of Keywords in Java

    Look at the following Java program:

    public class Main {
        public static void main(String[] args) {
            int age = 20;
    
            if (age >= 18) {
                System.out.println("Eligible");
            }
        }
    }

    In this program, several Java keywords are used.

    Keyword Purpose
    public Defines access level.
    class Used to define a class.
    static Allows a method or variable to belong to the class.
    void Means the method does not return a value.
    int Used to declare an integer variable.
    if Used for decision-making.

    Keyword vs Identifier

    Beginners often confuse keywords and identifiers. They are different.

    Keyword Identifier
    Predefined word in a programming language. Name created by the programmer.
    Has a fixed meaning. Used to name variables, methods, classes, etc.
    Cannot be changed. Can be chosen by the programmer.
    Cannot be used as variable name. Can be used as variable name if it follows naming rules.
    Example: int, if, class Example: age, totalMarks, Main
    Remember: Keywords are created by the programming language, while identifiers are created by the programmer.

    Why Keywords Cannot Be Used as Variable Names

    Keywords cannot be used as variable names because they already have special meanings in the language.

    Wrong Example

    int class = 10;

    This is incorrect because class is a Java keyword. Java uses class to define a class, so it cannot be used as a variable name.

    Correct Example

    int classNumber = 10;

    This is correct because classNumber is an identifier created by the programmer, and it is not a reserved keyword.

    Categories of Java Keywords

    Java keywords can be grouped into different categories based on their use.

    1

    Data Type Keywords

    Used to declare types of data.

    Examples include int, double, char, boolean, float, and long.

    2

    Control Flow Keywords

    Used to control the execution flow.

    Examples include if, else, switch, case, for, while, break, and continue.

    3

    Access Modifier Keywords

    Used to control visibility and access.

    Examples include public, private, and protected.

    4

    Class and Object Keywords

    Used in object-oriented programming.

    Examples include class, interface, extends, implements, new, this, and super.

    5

    Exception Handling Keywords

    Used to handle program errors.

    Examples include try, catch, finally, throw, and throws.

    1. Data Type Keywords

    Data type keywords are used to declare variables and define what type of value the variable can store.

    int age = 20;
    double price = 99.50;
    char grade = 'A';
    boolean isPassed = true;
    Keyword Meaning Example
    int Stores integer values. int age = 20;
    double Stores decimal values. double price = 99.50;
    char Stores a single character. char grade = 'A';
    boolean Stores true or false values. boolean isPassed = true;

    2. Control Flow Keywords

    Control flow keywords help a program make decisions, repeat tasks, and change execution flow.

    if and else

    int marks = 80;
    
    if (marks >= 35) {
        System.out.println("Pass");
    } else {
        System.out.println("Fail");
    }

    Here, if and else are keywords used for decision-making.

    for Loop

    for (int i = 1; i <= 5; i++) {
        System.out.println(i);
    }

    Here, for is a keyword used to repeat a block of code.

    while Loop

    int i = 1;
    
    while (i <= 5) {
        System.out.println(i);
        i++;
    }

    Here, while is a keyword used for loop-based repetition.

    3. Access Modifier Keywords

    Access modifier keywords define where a class, method, or variable can be accessed from.

    public class Main {
        private int age;
        protected String name;
    }
    Keyword Basic Meaning
    public Can be accessed widely.
    private Can be accessed only within the same class.
    protected Can be accessed within package and subclasses.

    4. Class and Object Keywords

    Java is an object-oriented programming language, so many keywords are used to define classes, objects, inheritance, and object behavior.

    class and new

    class Student {
        String name;
    }
    
    public class Main {
        public static void main(String[] args) {
            Student s1 = new Student();
        }
    }

    Here, class defines a class, and new creates an object.

    extends

    class Animal {
    }
    
    class Dog extends Animal {
    }

    Here, extends is used for inheritance.

    5. Exception Handling Keywords

    Exception handling keywords help manage runtime errors in a program.

    try {
        int result = 10 / 0;
    } catch (Exception e) {
        System.out.println("Error occurred");
    } finally {
        System.out.println("Program ended");
    }

    Here, try, catch, and finally are exception handling keywords.

    Common Java Keywords List

    The following table shows commonly used Java keywords that beginners should recognize first.

    Keyword Common Use Example
    class Defines a class. class Main
    public Access modifier. public class Main
    static Belongs to class. public static void main
    void No return value. void main
    int Integer data type. int age
    if Condition checking. if (marks >= 35)
    else Alternative condition block. else
    for Looping. for (int i = 1; i <= 5; i++)
    while Looping while condition is true. while (i <= 5)
    return Returns a value from method. return sum;

    Keywords and Case Sensitivity

    Java is case-sensitive. This means class and Class are not the same.

    Correct

    public class Main {
    }

    Incorrect

    Public Class Main {
    }

    The second example is incorrect because Java keywords must be written exactly as defined, usually in lowercase.

    Keywords, Literals, and Identifiers

    Keywords, literals, and identifiers are different parts of programming language syntax.

    Term Meaning Example
    Keyword Reserved word with predefined meaning. int, if, class
    Literal Fixed value written directly in code. 10, true, "Hello"
    Identifier Name created by programmer. age, totalMarks, Student

    Complete Java Example Using Keywords

    The following Java program uses several important keywords.

    public class Main {
        public static void main(String[] args) {
            int number = 10;
    
            if (number > 0) {
                System.out.println("Positive number");
            } else {
                System.out.println("Not positive");
            }
    
            for (int i = 1; i <= 3; i++) {
                System.out.println(i);
            }
        }
    }

    Output

    Positive number
    1
    2
    3

    Keyword Breakdown

    Keyword Used For
    public Access level.
    class Class declaration.
    static Class-level method.
    void No return value.
    int Integer variable declaration.
    if Decision-making.
    else Alternative branch.
    for Looping.

    Rules for Using Keywords

    Students should follow these rules while using keywords.

    Important Rules

    • Do not use keywords as variable names.
    • Do not use keywords as method names.
    • Do not use keywords as class names.
    • Write keywords exactly as defined by the language.
    • Remember that Java keywords are case-sensitive.
    • Use keywords only for their predefined purpose.
    • Learn keywords gradually with examples.
    • Do not memorize all keywords at once; understand them by usage.

    How Keywords Help Debugging

    Understanding keywords helps students identify syntax errors quickly. Many beginner errors happen because keywords are misspelled or used incorrectly.

    Debugging Questions

    • Is the keyword spelled correctly?
    • Is the keyword written in lowercase?
    • Is a keyword being used as a variable name?
    • Is the keyword placed in the correct location?
    • Is the keyword used with proper syntax?
    • Are braces and semicolons used correctly with keyword-based statements?

    Common Beginner Mistakes

    Mistakes

    • Using keywords as variable names.
    • Writing Public instead of public.
    • Writing Class instead of class.
    • Forgetting that Java is case-sensitive.
    • Confusing keywords with identifiers.
    • Trying to change the meaning of a keyword.
    • Using reserved words for class or method names.
    • Memorizing keywords without understanding their use.

    Better Habits

    • Use meaningful identifiers that are not keywords.
    • Write Java keywords in lowercase.
    • Understand keywords through examples.
    • Practice reading Java programs and identifying keywords.
    • Use IDE highlighting to recognize keywords.
    • Learn keywords category-wise.
    • Check compiler errors carefully.
    • Follow naming conventions for identifiers.

    Prerequisites Before Learning Keywords

    To understand keywords properly, students should know some basic programming concepts.

    Basic Prerequisites

    • Basic understanding of programming.
    • Basic program structure.
    • Statements in programming.
    • Expressions in programming.
    • Variables and data types.
    • Identifiers and naming rules.
    • Simple Java syntax.
    • Basic understanding of conditions and loops.

    Practice Activity: Identify Keywords

    This activity helps students identify keywords in a Java program.

    Task

    Read the following Java program and identify all keywords used in it.
    public class Main {
        public static void main(String[] args) {
            int marks = 75;
    
            if (marks >= 35) {
                System.out.println("Pass");
            } else {
                System.out.println("Fail");
            }
        }
    }

    Sample Answer

    Keyword Purpose
    public Access modifier.
    class Defines a class.
    static Used for class-level method.
    void Means no return value.
    int Declares integer variable.
    if Checks condition.
    else Executes alternative block.

    Mini Quiz

    1

    What is a keyword?

    A keyword is a reserved word that has a predefined meaning in a programming language.

    2

    Can keywords be used as variable names?

    No. Keywords cannot be used as variable names because they are reserved by the programming language.

    3

    Give three examples of Java keywords.

    Examples of Java keywords are class, int, and if.

    4

    What is the difference between keyword and identifier?

    A keyword is predefined by the language, while an identifier is a name created by the programmer.

    5

    Is Java case-sensitive?

    Yes. Java is case-sensitive, so class and Class are treated differently.

    Interview Questions on Keywords

    1

    Define keywords in programming.

    Keywords are predefined reserved words that have special meanings in a programming language and are used as part of its syntax.

    2

    Why can keywords not be used as identifiers?

    Keywords cannot be used as identifiers because they already have fixed meanings and are reserved by the programming language.

    3

    What are identifiers?

    Identifiers are names created by programmers for variables, methods, classes, and other program elements.

    4

    Give examples of control flow keywords in Java.

    Examples include if, else, switch, for, while, break, and continue.

    5

    What happens if we use a keyword as a variable name?

    The program will show a compilation error because the keyword is reserved and cannot be used as an identifier.

    Quick Summary

    Concept Meaning
    Keyword Reserved word with predefined meaning.
    Identifier Name created by programmer.
    Reserved Word A word that cannot be used for another purpose.
    Data Type Keyword Used to define type of data.
    Control Flow Keyword Used to control execution flow.
    Access Modifier Keyword Used to define access level.
    Case Sensitivity Java treats uppercase and lowercase differently.

    Final Takeaway

    Keywords are the reserved words of a programming language. They have fixed meanings and help define the structure, logic, and behavior of a program. In Java, keywords such as class, public, static, int, if, and for are essential for writing valid programs. Students should learn keywords by understanding their purpose and practicing them in real code examples.