Thursday , July 18 2019

40 Java Keywords With Examples

Share on WhatsApp
Share via email Share

1) 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 AbstractClass
{
	abstract void abstractMethod();
}

 

2) boolean

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

boolean isActive = true;

3) break

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

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

4) 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 = 50;

5) switch       6) case

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

public class MainClass
{
	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;
		}
	}
}

7) try     8) catch     9) 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. finallyblock is always executed.

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

10) 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';

11) class

class keyword is used to define the classes in java.

class MyClass
{
	class MyInnerClass
	{
		//Inner Class
	}
}

12) continue

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

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

13) 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 MyInterface 
{
    public default void myDefaultMethod() 
    {
        System.out.println("Default Method");
    }
}

14) do

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

public class MainClass
{
	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);
	}
}

15) double

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

public class MainClass
{
	public static void main(String[] args) 
	{
		double d1 = 23.56;
		
		double d2 = 56.23;
		
		double d3 = d1 + d2;
		
		System.out.println(d3);
	}
}

16) if         17) else

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

public class MainClass
{
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter a string :");
		
		String input = sc.next();
		
		if(input.equalsIgnoreCase("JAVA"))
		{
			System.out.println("It's JAVA");
		}
		else
		{
			System.out.println("It's not JAVA");
		}
	}
}

18) enum

enum keyword is used to define enum types.

enum MyEnums
{
	A, B, C, D;
}

19) extends

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

class SuperClass
{
	//Super Class
}

class SubClass extends SuperClass
{
	//Sub Class
}

20) 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 FinalClass
{
	final int finalVariable = 10;
	
	final void finalMethod()
	{
		//final method
	}
}

21) float

float keyword indicates primitive float type of variables.

public class MainClass
{
	public static void main(String[] args) 
	{
		float f1 = 45.26f;
		
		float f2 = 84.25f;
		
		float f3 = f2 - f1;
		
		System.out.println(f3);
	}
}

22) for

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

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

 

23) import

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

import java.sql.*;
import java.util.Arrays;
import java.util.Scanner;

24) instanceOf

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

class A
{
	
}

public class MainClass
{
	public static void main(String[] args) 
	{
		A a = new A();
		
		if(a instanceof A)
		{
			System.out.println("a is of type A");
		}
	}
}

25) int

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

public class MainClass
{
	public static void main(String[] args) 
	{
		int i1 = 10;
		
		int i2 = 20;
		
		int i3 = i1 *  i2;
		
		System.out.println(i3);
	}
}

26) interface

interface keyword is used to define the interfaces in java.

interface MyInterface
{
	void myMethod();
}

27) long

long is used to define the primitive long type variables.

public class MainClass
{
	public static void main(String[] args) 
	{
		long l1 = 101;
		
		long l2 = 202;
		
		long l3 = l1 +  l2;
		
		System.out.println(l3);
	}
}

 

28) new

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

class A
{
	
}

public class MainClass
{
	public static void main(String[] args) 
	{
		A a = new A();
	}
}

29) package

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

package pack1;

class A
{
	
}

30) 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 A
{
	private int i = 111;   //private field
	
	private void method()
	{
		//private method
	}
}

31) 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 A
{
	protected int i = 111;   //protected field
	
	protected void method()
	{
		//protected method
	}
}

32) 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 A
{
	public int i = 222;   //public field
	
	public A()
	{
		//public constructor
	}
	
	public void method()
	{
		//public method
	}
}

33) return

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

class A
{
	int method(int i)
	{
		return i*i;     //method returning a value
	}
}

34) short

short keyword is used to declare primitive short type variables.

short s1 = 11;
		
short s2 = 22;

35) 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 A
{
	static int staticField = 555;    //Static Field
	
	static void staticMethod()
	{
		//Static method
	}
}

public class MainClass
{
	public static void main(String[] args) 
	{
		System.out.println(A.staticField);    //Accessing staticField via class name
		
		A.staticMethod();     //Accessing staticMethod via class name
	}
}

 

36) super

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

class A
{
	int i;
	
	public A(int i) 
	{
		this.i = i;
	}
	
	void methodA()
	{
		System.out.println(i);
	}
}

class B extends A
{
	public B()
	{
		super(10);    //Calling super class constructor
	}
	
	void methodB()
	{
		System.out.println(super.i);    //accessing super class field
		
		super.methodA();    //Calling super class method
	}
}

 

37) this

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

class AnyClass
{
    int i;
 
    AnyClass()
    {
        System.out.println("First Constructor");
    }
 
    AnyClass(int j)
    {
        this();    //calling statement to First Constructor
        System.out.println("Second Constructor");
    }
 
    void methodOne()
    {
        System.out.println("From method one");
    }
 
    void methodTwo()
    {
        System.out.println(this.i);  //Accessing same class field
        this.methodOne();      //Accessing same class method
    }
}

 

 

 

38) while

while keyword is used in the while loop.

public class MainClass
{
	public static void main(String[] args) 
	{
		int i = 10;
		
		while (i <= 100)
		{
			System.out.println(i);
			
			i = i + 10;
		}
	}
}

39) goto        40) const

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

Share on WhatsApp
Share via email Share

About Zain-ul-Abdin

Zain-ul-Abdin
An Imaginary person

Check Also

Apple-IOS-vs-Android

Google Android 6.0 Marshmallow vs Apple iOS 9

Google Android 6.0 Marshmallow vs Apple iOS 9: The Features comparison In a month or …

Leave a Reply

Your email address will not be published.