Java Keywords with Examples





Hey guys in this post, we will discuss complete list of Java keywords and their examples.

abstract


abstract keyword is used to implement the abstraction in java. A method which doesn’t have method definition must be declared as abstract and the class containing it must be declared as abstract. You can’t instantiate abstract classes. Abstract methods must be implemented in the sub classes. You can’t use abstract keyword with variables and constructors.

abstract class AbstractKeyword {
	
	abstract void abstractMethod();
	
}

assert


assert keyword is used in the assertion statements. These statements will enable you to test your assumptions about a program. Assertion statements provide the best way to detect and correct the programming errors. Assertion statements take one boolean expression as input and assumes that this will be always true. If the boolean expression returns false, AssertionError will be thrown.

import java.util.Scanner;

public class AssertKeyword
{
    public static void main(String[] args) 
    {
        System.out.println("Enter your age");
         
        Scanner sc = new Scanner(System.in);
         
        int age = sc.nextInt();
         
        assert age > 18 : "You have right to vote";
    }
}

boolean


boolean keyword is used to define boolean type variables. boolean type variables can hold only two values – either true or false.

boolean isAdmin = true;

break


The break keyword is used to stop the execution of a loop(for, while, switch-case) based on some condition.

public class BreakKeyword
{
    public static void main(String[] args) 
    {
    	int j = 10;
        for (int i = 0; i < 100; i++)
        {
            System.out.println(i);
             
            if(i == j)
            {
                break;
            }
        }
    }
}

byte


byte keyword is used to declare byte type of variables. A byte variable can hold a numeric value in the range from -128 to 127.

byte b = 55;

switch and case


Both switch and case keywords are used in the switch-case statement.

import java.util.Scanner;

public class SwitchCaseKeyword
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
         
        System.out.println("Enter Day :");
         
        int day = sc.nextInt();
         
        switch (day)
        {
            case 1 : System.out.println("SUNDAY");
            break;
             
            case 2 : System.out.println("MONDAY");
            break;
 
            case 3 : System.out.println("TUESDAY");
            break;
             
            case 4 : System.out.println("WEDNESDAY");
            break;
             
            case 5 : System.out.println("THURSDAY");
            break;
             
            case 6 : System.out.println("FRIDAY");
            break;
             
            case 7 : System.out.println("SATURDAY");
            break;
             
            default: System.out.println("Invalid");
            break;
        }
    }
}

try, catch and finally


try, catch and finally keywords are used to handle the exceptions in java. The statements which are to be monitored for exceptions are kept in the try block. The exceptions thrown by the try block are caught in the catch block. finally block is always executed.

public class TryCatchFinallyKeyword
{
    public static void main(String[] args) 
    {
        try
        {
            int i = Integer.parseInt("1a2b3c");
        }
        catch(NumberFormatException ex)
        {
            System.out.println(ex);
        }
        finally
        {
            System.out.println("This will be always executed");
        }
    }
}

char


char keyword is used to declare primitive char type variables. char represents the characters in java.

char a = 'A';

char b = 'B';

char c = 'C';




class


class keyword is used to define the classes in java.

class ClassKeyword
{
    class InnerClass
    {
        //TODO
    }
}

continue


continue keyword is used to stop the execution of current iteration and start the execution of next iteration in a loop.

public class ContinueKeyword
{
    public static void main(String[] args) 
    {
        for (int i = 0; i <= 100; i++)
        {
            if(i % 2 != 0)
            {
                continue;
            }
             
            System.out.println(i);
        }
    }
}

default


default keyword is used to define the default methods in an interface (From Java 8). default keyword is also used in the switch-case statements.

interface DefaultKeyword
{
    public default void defaultMethod() 
    {
        System.out.println("Default Method calling");
    }
}

do


do keyword is used in a do–while loop. do-while loop is used to execute one or more statements repetitively until a condition returns false.

public class DoKeyword
{
    public static void main(String[] args) 
    {
        int a = 10;
         
        int b = 20;
         
        do
        {
            a = a + b;
             
            b = b + 10;
             
            System.out.println("a = "+a);
             
            System.out.println("b = "+b);
             
        } while (a <= 100);
    }
}

double


double keyword is used to declare primitive double type of variables.

double d = 10.5;

if and else


if and else keywords are used in if-else block.

if("Java".equals("JAVA")){
    System.out.println("It's Java");
}else {
    System.out.println("It's not Java");
}

enum


enum keyword is used to define enum types.

enum EnumKeyword {
    ADMIN, USER, SELLER, CUSTOMER
}




extends


extends keyword is used in inheritance. It is used when a class extends another class.

class Child extends Parent {

}

final


final keyword is used when a class or a method or a field doesn’t need further modifications. final class can’t be extended, final method can’t be overridden and the value of a final field can’t be changed.

final class FinalKeyword {
    public static final int finalField = 100;
    public final void finalMethod() {
       //TODO 
    }
}

float


float keyword indicates primitive float type of variables.

float f = 33.3f;

for


for loop is used to execute the set of statements until a condition is true.

for(int i = 0; i < 5; i++) {
    //TODO
}

implements


implements keyword is used while implementing an interface.

class ImplementsKeyword implements SomeInterface{
    //TODO
}

import


import keyword is used to import the members of a particular package into current java file.

import java.util.*;
import java.lang.*;

instanceOf


instanceOf is used to check whether an object is of specified type. The syntax for using instanceOf keyword is “Object_Reference instanceOf Type“.

if(employee instanceOf Employee){
    //TODO
}

int


int keyword is used to declare primitive integer type of variables.

int i = 1000;

interface


interface keyword is used to define the interfaces in java.

interface InterfaceKeyword {
    //TODO
}

long


long is used to define the primitive long type variables.

long l = 80L;

native


native keyword is used with a method to indicate that a particular method is implemented in native code using Java Native Interfaces(JNI).

class NativeKeyword {
    public native void someMethod();
}

new


new keyword is used while creating the instances of a class.

Employee e = new Employee();

package


package keyword is used to specify a package to which the current file belongs to.

package in.bushansirgur.springbootrestapi;




private


private keyword is used to declare a member of a class as private. private methods and fields are visible within the class in which they are defined.

class PrivateKeyword{
    private int i = 123;
    private void privateMethod() {
    //TODO
    }
}

protected


protected keyword is used to declare a member of a class as protected. protected members of a class are visible within the package only, but they can be inherited to any sub classes.

class ProtectedKeyword {
    protected int i = 123;
    protected void protectedMethod() {
    //TODO
    }
}

public


public keyword is used to declare the members of a class or class itself as public. public members of a class are visible from anywhere and they can be inherited to any sub classes.

public class PublicKeyword {
    public int i = 123;
    public PublicKeyword() {
    //TODO
    } 
    public void publicMethod() {
    //TODO
    }
}

return


return keyword is used to return the control back to the caller from the method.

int returnValue() {
    return 1 + 1;
}

short


short keyword is used to declare primitive short type variables.

short s = 34;

static


static keyword is used to define the class level members of a class. static members of a class are stored in the class memory and you can access them directly through class name. No need to instantiate a class.

class StaticKeyword {
    public static int staticField = 123;
    public static staticMethod() {
    //TODO
    }
}

strictfp


strictfp keyword is used to implement the strict precision of floating point calculations on different platforms. strictfp can be used with classes, interfaces and methods.

strictfp StrictfpInterface {
    //TODO
}
strictfp class StrictfpClass {
    strictfp void strictfpMethod(){
    //TODO
    }
}

super


super keyword is used to access super class members inside a sub class.

public Employee() {
    super(100);
}
public someMethod() {
    super.parentMethod();
}

synchronized


synchronized keyword is used to implement the synchronization in java. only one thread can enter into a method or a block which is declared as synchronized. Any thread which wants to enter synchronized method or block must acquire object lock of those methods or blocks.

synchronized void synchronizedMethod() {
    //TODO
}




this


this keyword is used to access other members of the same class.

this();
this.i;

throw


throw keyword is used to throw the exceptions manually.

public class ThrowKeyword{
   public static void main(String args[]) {
      //TODO
      throw new ResourceNotFoundException("Employee not found for the id"+id);
   }
}

throws


throws keyword is used to specify the exceptions which the current method may throw.

class ThrowsKeyword {
    void someMethod() throws ResourceNotFoundException{
    //TODO
    }
}

transient


transient keyword is used in serialization. A variable which is declared as transient will not be eligible for serialization.

transient int i;

void


void keyword is used to indicate that method returns nothing.

void voidMethod(){
   //TODO
}

volatile


volatile keyword is used in the concurrent programming. The value of a variable which is declared as volatile will be written into or read from the main memory.

public volatile int timer= 30;

while


while keyword is used in the while loop.

while(i == 5) {
   j = "THURSDAY";
}

goto and const


Both goto and const are reserved words in java but they are currently not used.



Partners: Sultanbet Casino spillesider med dansk licens casino ohne anmeldung sofort auszahlung funky time play ny dansk casino casino sofortauszahlung Richard Casino casino ohne 1000 limit BC.Game Casino neteller nederland

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply