All 50 Java Keywords with Examples


Java Language Keywords

This article describes the list of keywords in the Java programming language. The keywords const and goto are reserved, even though they are not currently used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.
Learn complete free Java programming language at Java Tutorial | Learn Java Programming with Examples



abstract Java Keyword

The abstract keyword is used to declare a class or a method as abstract. An abstract class is a class that is declared abstract means it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without an implementation.
Read more about abstract keyword with a complete example at abstract Keyword in Java with Examples

assert Java Keyword

Assert describes a predicate (a true-false statement) placed in a Java program to indicate that the developer thinks that the predicate is always true at that place. If an assertion evaluates to false at run-time, an assertion failure results, which typically causes execution to abort. Optionally enable by ClassLoader method.
Read more about abstract keyword with a complete example at assert Keyword in Java with Examples

boolean Java Keyword

Defines a boolean variable for the values "true" or "false" only. By default, the value of boolean primitive type is false. This keyword is also used to declare that a method returns a value of the primitive type boolean.

Read more about boolean keyword with a complete example at boolean Keyword in Java with Examples

break Java Keyword

The break keyword is used to prematurely exit a forwhile, or do while loop or to mark the end of a case block in a switch statement.

Read more about break keyword with a complete example at break Keyword in Java with Examples

byte Java Keyword

The byte keyword is used to declare a field that can hold an 8-bit signed two's complement integer. This keyword is also used to declare that a method returns a value of the primitive type byte.

Read more about byte keyword with a complete example at byte Keyword in Java with Examples

case Java Keyword

A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.

Read more about case keyword with a complete example at Java Switch Case Statement with Examples

catch Java Keyword

Used in conjunction with a try block and an optional finally block. The statements in the catch block specify what to do if a specific type of exception is thrown by the try block.

Read more about catch keyword with a complete example at Java try/catch Block

char Java Keyword

  1. The char keyword is used to declare character variable. 
  2. char is a Java primitive type. 
  3. In Java, the data type used to store characters is char. The char data type is a single 16-bit Unicode character.
  4. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
  5. The Wrapper Character class represents char primitive type as an object.
Read more about char keyword with a complete example at char Keyword in Java with Examples

class Java Keyword

The class keyword is used to define the classes in JavaA class is a group of objects which have common properties. It is a template or blueprint from which objects are created. In short, a class is the specification or template of an object.

Read more about class keyword with a complete example at What is Class in Java with Programming Examples

continue Java Keyword

Used to resume program execution at the end of the current loop body. If followed by a label, continue resumes execution at the end of the enclosing labeled loop body.

The continue statement skips the current iteration of a forwhile or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop.

Read more about continue keyword with a complete example at Java continue Statement with Examples

default Java Keyword

The default keyword can optionally be used in a switch statement to label a block of statements to be executed if no case matches the specified value; see a switch. Alternatively, the default keyword can also be used to declare default values in a Java annotation. From Java 8 onwards, the default keyword is also used to specify that a method in an interface provides the default implementation of a method.

Read more about default keyword with a complete example at Java Switch Case Statement with Examples

do Java Keyword

The do keyword is used in conjunction with while to create a do-while loop, which executes a block of statements associated with the loop and then tests a boolean expression associated with the while. If the expression evaluates to true, the block is executed again; this continues until the expression evaluates to false.

Read more about do keyword with a complete example at Java do while Loop with Examples.

double Java Keyword

  1. The double keyword is used to create a double primitive type.
  2. double variable may store a double−precision floating point value. Double precision, as denoted by the double keyword, uses 64 bits to store a value. 
  3. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations.
  4. The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.
Read more about default keyword with a complete example at double Java Keyword with Examples.

else Java Keyword

The Java if-else statement also tests the condition. It executes the if block if a condition is true otherwise else block, is executed.

Syntex

if(condition){  
     statement 1; //code if condition is true  
}else{  
     statement 2; //code if condition is false  
}  
Read more about else keyword with a complete example at Java if, if else, nested if, if else if Statement with Examples.

enum Java Keyword

An enum Java keyword used to declare an enumerated type. Enumerations extend the base class Enum.

public enum Day {
 SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY;
}

Read more about enum keyword with a complete example at Enums in Java.

extends Java Keyword

The extends keyword is used in a class or interface declaration to indicate that the class or interface being declared is a subclass of the class or interface whose name follows the extends keyword.

Read more about extends keyword with a complete example at extends Java Keyword with Examples.

final Java Keyword

The final keyword in Java is used to restrict the user. The final keyword can be used with variable, method, and class.
  1. The final keyword may be applied to a variable, indicating the value of the final variable can not be changed (It will be constant).
  2. The final keyword may be applied to a class, indicating the class may not be extended (subclassed).
  3. The final keyword may be applied to a method, indicating the method may not be overridden in any subclass
Read more about final keyword with a complete example at final Java Keyword with Examples.

finally Java Keyword

The finally used to define a block of statements for a block defined previously by the try keyword. The finally block is executed after execution exits the try block and any associated catch clauses regardless of whether an exception was thrown or caught, or execution left method in the middle of the try or catch blocks using the return keyword.

  • Java finally block is a block that is used to execute important code such as closing connectionstreametc.
  • Java finally block is always executed whether an exception is handled or not.
  • Java finally block follows try/catch block.
  • For each try block, there can be zero or more catch blocks, but only one finally block.
  • The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).
Read more about finally keyword with a complete example at Java finally Block.

float Java Keyword

The float keyword is used to declare float primitive variable. A float variable may store a single−precision floating point value. The type float specifies a single-precision value that uses 32 bits of storage. Single precision is faster on some processors and takes half as much space as double precision, but will become imprecise when the values are either very large or very small.

Read more about float keyword with a complete example at float Java Keyword with Examples.

for Java Keyword

The for keyword is used to create a for loop, which specifies a variable initialization, a boolean expression, and an incrementation. The variable initialization is performed first, and then the boolean expression is evaluated. If the expression evaluates to true, the block of statements associated with the loop is executed, and then the incrementation is performed. The boolean expression is then evaluated again; this continues until the expression evaluates to false.

Read more about for keyword with a complete example at Java for Loop with Examples.

if Java Keyword

The if keyword is used to create an if statement, which tests a boolean expression; if the expression evaluates to true, the block of statements associated with the if statement is executed. This keyword can also be used to create an if-else statement.

Read more about if keyword with a complete example at Java if, if else, nested if, if else if Statement with Examples.

implements Java Keyword

The implements keyword is used in a class declaration to indicate that the class being declared provides implementations for all methods declared in the interface whose name follows the implements keyword.

Read more about implements keyword with a complete example at implements Java Keyword with Examples.

import Java Keyword

The import keyword makes one class or all classes in a package visible in the current Java source file. Imported classes can be referenced without the use of fully−qualified class names.
In simple words, if a class wants to use another class in the same package, the package name does not need to be used. Classes in the same package find each other.

Read more about import keyword with a complete example at import Java Keyword with Examples.

instanceof Java Keyword

The Java instanceof keyword is used to test whether the object is an instance of the specified type (class or subclass or interface).
Syntex:
obj instanceOf Object 
The left side is the instance and right side is the Java class name. Java instanceof operator returns a boolean result.
The instanceof in Java is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has a null value, it returns false.
Read more about instanceof keyword with a complete example at instanceof Java Keyword with Examples.

int Java Keyword

  1. The int keyword is used to declare a variable as a numeric type. For example, int a = 10;
  2. int variable is a signed 32-bit type that has a range from –2,147,483,648 to 2,147,483,647.
  3. The Integer class is a wrapper class for the int primitive type. It defines MIN_VALUE and MAX_VALUE constants representing the range of values for this type.
Read more about int keyword with a complete example at int Java Keyword with Examples.

interface Java Keyword

Used to declare a special type of class that only contains abstract or default methods, constant (static final) fields and static interfaces. It can later be implemented by classes that declare the interface with the implements keyword. As multiple inheritance is not allowed in Java, interfaces are used to circumvent it. An interface can be defined within another interface.

public interface Vehicle {
    String getBrand();

    String speedUp();

    String slowDown();

    default String turnAlarmOn() {
        return "Turning the vehice alarm on.";
    }

    default String turnAlarmOff() {
        return "Turning the vehicle alarm off.";
    }

    static int getHorsePower(int rpm, int torque) {
         return (rpm * torque) / 5252;
     }
}

Read more about interface keyword with a complete example at Guide to Java Interface.

long Java Keyword

  1. The long keyword is used to declare a variable as a long primitive type.
  2. The long primitive type is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value.
  3. The Long class is a wrapper class for the long primitive type. It defines MIN_VALUE and MAX_VALUE constants representing the range of values for this type.
Read more about long keyword with a complete example at long Java Keyword with Examples.

native Java Keyword

  1. The native keyword may be applied to a method to indicate that the method is implemented in a language other than Java.
  2. The native keyword is used to declare a method which is implemented in platform-dependent code such as C or C++. When a method is marked as native, it cannot have a body and must ends with a semicolon instead.
  3. The Java Native Interface (JNI) specification governs rules and guidelines for implementing native methods, such as data type conversion between Java and the native application.
Read more about native keyword with a complete example at native Java Keyword with Examples.

new Java Keyword

The new keyword is used to create a new instance of a class.

The new Java Keyword Examples
  1. The new keyword is used to create a new instance of a class.
Student student = new Student("Tom", 20);
  1. The new keyword can be used to create a new array object:
// use the new keyword to create an int array object
int[] intArray = new int[10];
  1. The new keyword can be used to create a new String array object:
// use the new keyword to create a String object
String string = new String();
Read more about new keyword with a complete example at new Java Keyword with Examples.

package Java Keyword

Java package is a group of similar classes and interfaces. Packages are declared with the package keyword.

For example:

package java.lang;
package java.util;

Read more about package keyword with a complete example at Packages in Java with Examples.

private Java Keyword

The private keyword is used in the declaration of a method, field, or inner class; private members can only be accessed by other members of their own class

Read more about private keyword with a complete example at private Java Keyword with Examples.

protected Java Keyword

The protected keyword is used in the declaration of a method, field, or inner class; protected members can only be accessed by members of their own class, that class's subclasses or classes from the same package

Read more about protected keyword with a complete example at protected Java Keyword with Examples.

public Java Keyword

The public keyword is an access control modifier that may be applied to a class, a method or a field (a variable declared in a class).
If a class or its members are declared as public, they can be accessed from any other class regardless of the package boundary. It is comparable to a public place in the real world, such as a company cafeteria that all employees can use irrespective of their department.
Read more about public keyword with a complete example at public Java Keyword with Examples.

return Java Keyword

The return keyword causes a method to return to the method that called it, passing a value that matches the return type of the returning method.

Read more about public keyword with a complete example at return Java Keyword with Examples.

short Java Keyword

  1. The short keyword is used to declare a variable as a numeric type.
  2. short is a signed 16-bit type.
  3. It has a range from –32,768 to 32,767.
  4. The Short class is a wrapper class for the short primitive type. It defines MIN_VALUE and MAX_VALUE constants representing the range of values for this type.
Read more about short keyword with a complete example at short Java Keyword with Examples.

static Java Keyword

The static keyword is used to declare a field, method, or inner class as a class field. Classes maintain one copy of class fields regardless of how many instances exist of that class. static also is used to define a method as a class method. Class methods are bound to the class instead of to a specific instance, and can only operate on class fields. 

Read more about static keyword with a complete example at static Keyword in Java with Examples.

strictfp Java Keyword

strictfp is a keyword in the Java programming language that restricts floating-point calculations to ensure portability.

Read more about strictfp keyword with a complete example at strictfp.

super Java Keyword

The super keyword in Java is a reference variable that is used to refer parent class object.
The super has two general forms:
  • The first calls the superclass constructor.
  • The second is used to access methods or instance variables of the superclass that has been hidden by a member of a subclass.
Read more about super keyword with a complete example at super Keyword in Java with Examples.

switch Java Keyword

The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. As such, it often provides a better alternative than a large series of if-else-if statements.

Read more about switch keyword with a complete example at Java Switch Case Statement with Examples.

synchronized Java Keyword

Used in the declaration of a method or code block to acquire the mutex lock for an object while the current thread executes the code.

We can synchronize our code in either of two ways. Both involve the use of the synchronized keyword.
  1. Using Synchronized Methods
  2. Using Synchronized Statement Or Block
Read more about synchronized keyword with a complete example at synchronized Java Keyword with Examples.

this Java Keyword

In Java, this is a reference variable that refers to the current object.

Read more about this keyword with a complete example at this Keyword in Java with Examples.

throw Java Keyword

Causes the declared exception instance to be thrown. This causes execution to continue with the first enclosing exception handler declared by the catch keyword to handle an assignment compatible exception type. If no such exception handler is found in the current method, then the method returns and the process is repeated in the calling method. If no exception handler is found in any method call on the stack, then the exception is passed to the thread's uncaught exception handler.

Read more about throw keyword with a complete example at Java throw Keyword with Examples.

throws Java Keyword

The Java throws keyword is used to declare an exception. It gives information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained.

Read more about throws keyword with a complete example at throws Java Keyword.

transient Java Keyword

Java transient keyword is used in serialization. If you define any data member as transient, it will not be serialized.

Let's take an example, I have declared a class as Employee, it has three data members id, name, and age. If you serialize the object, all the values will be serialized but I don't want to serialize one value, e.g. age then we can declare the age data member as transient.

Read more about transient keyword with a complete example at Java Transient Keyword with Examples.

try Java Keyword

Enclose the code that might throw an exception within a try block. If an exception occurs within the try block, that exception is handled by an exception handler associated with it. The try block contains at least one catchblock or finally block.

Read more about transient keyword with a complete example at try Java Keyword.

void Java Keyword

The void keyword is used to declare that a method does not return any value.

Read more about void keyword with a complete example at void Java Keyword.

volatile Java Keyword

Used in field declarations to specify that the variable is modified asynchronously by concurrently running threads. Methods, classes, and interfaces thus cannot be declared volatile, nor can local variables or parameters.

Read more about void keyword with a complete example at volatile Java Keyword.

while Java Keyword

The while keyword is used to create a while loop, which tests a boolean expression and executes the block of statements associated with the loop if the expression evaluates to true; this continues until the expression evaluates to false. This keyword can also be used to create a do-while loop.

Simple while Loop Example


Here is a while loop that counts down from 10, printing exactly ten lines of "tick":
package net.javaguides.corejava.controlstatements.loops;

public class WhileLoopExample {
    public static void main(String args[]) {
        int n = 10;
        while (n > 0) {
            System.out.println("tick " + n);
            n--;
        }
    }
}
Output:
tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1

goto Java Keyword

Although goto keyword is reserved as a keyword in Java, that goto is not used and has no function.

const Java Keyword

Although const keyword is a reserved in Java, const is not used and has no function. For defining constants in Java, see the final keyword.
Learn complete free Java programming language at Java Tutorial | Learn Java Programming with Examples

Comments