Keyword in Java Part -1

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.

abstract: It is a non-access modifier that is used for classes and methods. This will achieve one of OOP's concepts i.e Abstraction.

Example:

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

        //some logic
    }

}


boolean: A data type that holds true and false values.

Example

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

        boolean variable=true;
        System.out.println(variable);
    }
}


break: A control statement for breaking the loops. 

Example

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

        int i = 1, n = 6;
        while (i < n) {
            if (i == 2) {
                break;
            }
            i++;
        }
    }
}


assert: It is a java keyword used to define an assert statement. If the program is running with assertions enabled, then the condition is checked at runtime. If the condition is false, the Java runtime system throws an AssertionError

Example

import java.util.ArrayList;
import java.util.List;

public class MyClass {
    public static void main(String[] args) {
       
        List<String> list=new
ArrayList<String>();
        list.add("Hello");
        assert list != null &&
list.size() > 0 : "Hello list";
    }
}


final: Indicates that a variable holds a constant value or that a method will not be overridden. It is a non-access modifier used for classes, attributes, and methods, which makes them non-changeable (impossible to inherit or override). The final keyword is useful when you want a variable to always store the same value.

Example

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

        final String name = "NAME";
        System.out.println(name);
    }
}


finally: Indicates a block of code in a try-catch structure that will always be executed. It is used to execute code (used with exceptions - try..catch statements) no matter if there is an exception or not.

Example

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

        try {
            int[] myNumbers = { 1, ,7,8,9 };
            System.out.println(myNumbers[5]);
        } catch (Exception e) {
            System.out.println("Error occured");
        } finally {
            System.out.println("Try and catch block excecuted.");
        }
    }
}


float: A data type that holds a 32-bit floating-point number. The float keyword is a data type that can store fractional numbers from 3.4e−038 to 3.4e+038.

Example

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

        float value = 189.78f;

        System.out.println(value);
    }
}

     

for: It is used to start a for loop, in other words by this we can iterate an array or list.

Example

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

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

           

                                                                                                                              Next



Related Posts

Java Keyword Part -2

Java Keywords Part -3

Java Keywords Part -4

Java Keywords Part -5

Java Keywords Part -6 

No comments:

Post a Comment