Keywords in Java Part -4

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.

int: A data type that can hold a 32-bit signed integer. The int keyword is a data type that can store whole numbers from -2147483648 to 2147483647.

Example

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

        int number = 10;
        System.out.println(number);
    }
}


interface: Declares an interface. The interface keyword is used to declare a special type of class that only contains abstract methods.

Example

public interface MyClass {

    void display();

    int sum(int a, int b);
}


long: A data type that holds a 64-bit integer. The long keyword is a data type that can store whole numbers from -9223372036854775808 to 9223372036854775808.

Example

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

        long number = 101001001L;
        System.out.println(number);
    }
}


native: Specifies that a method is implemented with native (platform-specific) code. The native keyword in Java is applied to a method to indicate that the method is implemented in native code using JNI (Java Native Interface). 

Example

public class DateTimeUtils {
    public native String getSystemTime();

    static {
        System.loadLibrary("nativedatetimeutils");
    }
}


new: It is used to create a new object.

Example

public class MyClass {

    int i = 5;
    public static void main(String[] args) {

        MyClass myObj = new MyClass();

        System.out.println(myObj.i);
    }
}


null: This indicates that a reference does not refer to anything.

Example

public class MyClass {

    int i = 5;

    public static void main(String[] args) {

        MyClass myObj = null;

        System.out.println(myObj);
    }
}


package: Declares a Java package. It declares a 'name space' for the Java class. It must be put at the top of the Java file, it should be the first Java statement line.

Example

package com.oops;

public class MyClass {

    int i = 5;

    public static void main(String[] args) {

        MyClass myObj = null;

        System.out.println(myObj);
    }
}


private: The private keyword is an access modifier used for attributes, methods, and constructors, making them only accessible within the declared class.

Example

public class MyClass {

    private int i;

    public static void main(String[] args) {

        MyClass myObj = new MyClass();

        myObj.setI(10);

        System.out.println(myObj.getI());
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }
}


Prev                                                                                                                                              Next


Related Posts

Java Keywords Part -1

Java Keyword Part -2

Java Keywords Part -3

Java Keywords Part -5

Java Keywords Part -6

No comments:

Post a Comment