Keyword in Java Part -3

Keywords or reserved words are those which are basically used for internal processes. We cannot create a variable name as reserved keywords.

Below are some of the reserved keywords in java.

class: It is used to declare a new class, which is a collection of related variables and/or methods.

Example

public class MyClass {
    public static void main(String[] args) {

        // some logic
    }
}


continue: It is used to send the control back outside the loop. The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

Example

public class MyClass {
    public static void main(String[] args) {

        for (int i = 0; i < 5; i++) {
            if (i == 2) {
                continue;
            }
            System.out.println(i);
        }
    }
}


default: Specifies the default block of code in a switch statement. The default keyword specifies some code to run if there is no case match in the switch.

Example

public class MyClass {
    public static void main(String[] args) {

        String key = "Hello";
        switch (key) {
        case "Java":
            System.out.println("Java");
            break;
        default:
            System.out.println("Default block");
            break;
        }
    }
}


do: The do keyword is used together with while to create a do-while loop. Starts a do-while loop. 

Example

public class MyClass {
    public static void main(String[] args) {

        int i = 1, n = 5;

        do {
            System.out.println(i);
            i++;
        } while (i < n);
    }
}


double: A data type that can hold 64-bit floating-point numbers. The double keyword is a data type that can store fractional numbers from 1.7e−308 to 1.7e+308.

Note that you should end the value with a "d".

Example

public class MyClass {
    public static void main(String[] args) {

        double data = 19967.9d;
       
        System.out.println(data);
    }
}


else: It indicates alternative branches in an if statement. In other words, the else statement specifies a block of Java code to be executed if a condition is false in an if statement.

Example

public class MyClass {
    public static void main(String[] args) {

        int a = 2, b = 5;
        if (a > b) {
            System.out.println("a is greater than b");
        } else {
            System.out.println("b is greater than a");
        }
    }
}


enum: A Java keyword is used to declare an enumerated type. The enum keyword declares an enumerated (unchangeable) type.

Note

1. An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).

2. Note that they should be in uppercase letters.

How to create an enum.

To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma. 

Example

public enum MyEnum {

    ONE, TWO, THREE
}


extends: It indicates that a class is derived from another class or interface. It is used in inheritance or in other words, it will use to achieve the inheritance in java.

Create one parent class.

public class ParentClass {

    void display() {

    }

}


Example

public class MyClass extends ParentClass {

    public static void main(String[] args) {

    }
}


Prev                                                                                                                                             Next


Related Posts

Java Keywords Part -1

Java Keyword Part -2

Java Keywords Part -4

Java Keywords Part -5

Java Keywords Part -6

No comments:

Post a Comment