Showing posts with label Basic Programming. Show all posts
Showing posts with label Basic Programming. Show all posts

Keywords in Java Part -6

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.

synchronized: Specifies critical sections or methods in multithreaded code. The process of allowing only a single thread to access the shared data or resource at a particular point of time is known as Synchronization.

Example

public class MyClass {

    private int count;

    public static void main(String[] args) {

        MyClass myObj = new MyClass();
        myObj.setCount(10);

        myObj.increment(1);
        System.out.println(myObj.getCount());
    }

    public synchronized void increment(int value) {
        this.setCount(this.getCount() + value);
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}


this: Refers to the current object in a method or constructor. The most common use of this keyword is to eliminate the confusion between class attributes and parameters with the same name

Example

public class MyClass {

    private String name;

    public static void main(String[] args) {

        MyClass myObj = new MyClass();
        myObj.setName("Ram");

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}


throw: Creates an exception. The throw keyword is used to create a custom error.

Example

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

        int age = 16;

        if (age < 18) {
            throw new ArithmeticException("Access denied");
        } else {
            System.out.println("Access granted");
        }
    }
}


throws: Indicate what exceptions may be thrown by a method. It also handles multiple exceptions.


transient: Specifies that a variable is not part of an object’s persistent state. In other words, we can make a variable transient if we don't want to save its value in the file.

Example

import java.io.Serializable;

public class MyClass implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    transient int age = 16;

    public static void main(String[] args) {

        MyClass myObj = new MyClass();
        System.out.println(myObj.age);

    }
}


try: Starts a block of code that will be tested for exceptions.

Example

public class MyClass {

    public static void main(String[] args) {
        try {
            int[] myNumbers = { 1, 12, 6, 8 };
            System.out.println(myNumbers[5]);
        } catch (Exception e) {
            System.out.println("Exception occured");
        }
    }
}


void: Specifies that a method does not have a return value.

Example

public class MyClass {

    public static void main(String[] args) {

        MyClass myObj = new MyClass();
        myObj.printHello();
    }

    private void printHello() {

        System.out.println("Hello");
    }
}


volatile: This indicates that a variable may change asynchronously

Example

public class MyClass {
    static volatile int var = 5;

    public static void main(String[] args) {

        System.out.println(var);
    }

}


while: Starts a while loop. The while loop loops through a block of code as long as a specified condition is true:

Example

public class MyClass {

    public static void main(String[] args) {

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

}


Prev


Related Posts

Java Keywords Part -1

Java Keyword Part -2

Java Keywords Part -3

Java Keywords Part -4

Java Keywords Part -5


Keywords in Java Part -5

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.

protected: An access specifier indicating that a method or variable may only be accessed in the class it’s declared in (or a subclass of the class it’s declared in or other classes in the same package)

Example

public class MyClass {

    protected int i = 6;

    public static void main(String[] args) {

        MyClass myObj = new MyClass();

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

}


public: An access specifier used for classes, interfaces, methods, and variables indicating that an item is accessible throughout the application (or where the class that defines it is accessible)

Example

public class MyClass {

    public int i = 6;

    public static void main(String[] args) {

        MyClass myObj = new MyClass();

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

}


return: Sends control and possibly a return value back from a called method. In other words, come out of the method by returning some value or -1.

Example

public class MyClass {

    public static void main(String[] args) {

        MyClass myObj = new MyClass();

        System.out.println(myObj.sum(10, 20));
    }

    private int sum(int a, int b) {
        return a + b;
    }

}


short: A data type that can hold a 16-bit integer. The short keyword is a data type that can store whole numbers from -32768 to 32767.

Example

public class MyClass {

    public static void main(String[] args) {

        short value = 10;

        System.out.println(value);
    }

}


static: Indicates that a variable or method is a class method (rather than being limited to one particular object). Static methods/attributes can be accessed without creating an object of a class.

Example: 

The main method is the static method.

public class MyClass {

    public static void main(String[] args) {

        short value = 10;

        System.out.println(value);
    }

}


strictfp: Java strictfp keyword ensures that you will get the same result on every platform if you perform operations in the floating-point variable. 

Example

strictfp public class MyClass {

    public static void main(String[] args) {

        float a = 1009.1991f, b = 19019.1991f;

        float answer = a + b;

        System.out.println(answer);
    }

}


super: It refers to a class’s base class or parent class. It is used to call superclass methods, and to access the superclass constructor.

Example

class Parent {
    public void animalSound() {
        System.out.println("Parent is called");
    }
}

class Child extends Parent {
    public void animalSound() {
        super.animalSound(); // call the parent
        System.out.println("Child is called");
    }
}

public class MyClass {
    public static void main(String args[]) {
        Parent myDog = new Child();
        myDog.animalSound();
    }
}


switch: A statement that executes code based on a test value. In other words, it executes one statement from multiple ones. So we can say it is like an if-else-if ladder statement. 

Example

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

        String day = "Monday";
        switch (day) {
        case "Monday":
            System.out.println("Today is Monday");
            break;
        case "Tuesday":
            System.out.println("Today is Tuesday");
            break;
        case "Wednesday":
            System.out.println("Today is Wednesday");
            break;
        case "Thursday":
            System.out.println("Today is Thursday");
            break;
        case "Friday":
            System.out.println("Today is Friday");
            break;
        case "Saturday":
            System.out.println("Today is Saturday");
            break;
        case "Sunday":
            System.out.println("Today is Sunday");
            break;
        default:
            System.out.println("Not a day of week");
            break;
        }
    }
}


Prev                                                                                                                                              Next



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

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