Introduction to Java Keywords with Examples

Answered using GPT-4o-mini by Java Q&A Hub

Question:

Introduce Java Keywords and give examples

Answer:

Java keywords are reserved words that have a predefined meaning in the Java programming language. These keywords cannot be used as identifiers (names for variables, classes, methods, etc.) because they are part of the syntax of the language. Java has a total of 50 keywords, and they are case-sensitive.

Here’s a list of some commonly used Java keywords along with brief explanations and examples:

1. class

Defines a new class.

class MyClass {
    // class body
}

2. public

An access modifier indicating that the class, method, or variable is accessible from any other class.

public class MyClass {
    public void myMethod() {
        // method body
    }
}

3. private

An access modifier indicating that the method or variable is accessible only within its own class.

class MyClass {
    private int myVariable;
}

4. protected

An access modifier indicating that the method or variable is accessible within its own package and by subclasses.

class MyClass {
    protected void myMethod() {
        // method body
    }
}

5. static

Indicates that a method or variable belongs to the class rather than instances of the class.

class MyClass {
    static int myStaticVariable;
    
    static void myStaticMethod() {
        // method body
    }
}

6. void

Specifies that a method does not return a value.

public void myMethod() {
    // method body
}

7. int, double, boolean, etc.

Primitive data types.

int myInt = 10;
double myDouble = 5.5;
boolean myBoolean = true;

8. if, else

Control flow statements for conditional execution.

if (myInt > 5) {
    // do something
} else {
    // do something else
}

9. for, while

Looping statements.

for (int i = 0; i < 10; i++) {
    // loop body
}

while (myInt < 10) {
    // loop body
}

10. return

Exits from a method and optionally returns a value.

public int myMethod() {
    return 5;
}

11. try, catch, finally

Used for exception handling.

try {
    // code that may throw an exception
} catch (Exception e) {
    // handle exception
} finally {
    // code that will always execute
}

12. new

Used to create new objects.

MyClass myObject = new MyClass();

13. this

Refers to the current instance of a class.

public MyClass(int myVariable) {
    this.myVariable = myVariable; // 'this' refers to the instance variable
}

14. super

Refers to the superclass of the current object.

public class SubClass extends SuperClass {
    public SubClass() {
        super(); // calls the constructor of the superclass
    }
}

15. interface

Defines a new interface.

interface MyInterface {
    void myMethod();
}

16. implements

Used to implement an interface in a class.

class MyClass implements MyInterface {
    public void myMethod() {
        // method body
    }
}

17. extends

Used to indicate that a class is inheriting from a superclass.

class SubClass extends SuperClass {
    // class body
}

18. enum

Defines a set of named constants.

enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

19. abstract

Indicates that a class or method is abstract and must be implemented by subclasses.

abstract class MyAbstractClass {
    abstract void myAbstractMethod();
}

20. final

Indicates that a variable, method, or class cannot be modified or overridden.

final int MY_CONSTANT = 10;

final class MyFinalClass {
    // class body
}

These are just a few examples of Java keywords. Each keyword plays a specific role in the language's syntax and structure, and understanding them is essential for writing effective Java code.