Keywords in Java

Keywords in Java are the reserved tokens, each keyword having a predefined meaning in the java language. A keyword cannot be used as an identifier.. like name of a variable, function name etc.

All keywords are in lower case letters. In addition, null, true, and false denote literal values and like keywords they also cannot be used as identifiers.

Currently, there are 50 reserved keywords in java programming language.

abstract assert boolean break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
volatile while

Here is the brief description of each java keyword

  • abstract
    Keyword abstract is used to implement the abstraction in java. It can be applied to a class and methods. An abstract method only have declaration without definition(method body) and the class containing abstract methods must be declared as abstract. We cannot create objects of abstract classes and abstract methods must be implemented in the sub classes.

  • assert
    Keyword assert is used to define an assert statement. An assert statement is used to define a boolean expectation in a java program. If during runtime, an assertion statement evaluates to false, then Java runtime system throws a AssertionError.

  • boolean
    boolean keyword is used to define variables of boolean basic data types in java. boolean variables have only two possible values, true and false.

  • break
    break keyword is used to stop the execution of a loop and to take control out of the loop's body. It is also used to jump out of a switch case statement.

  • byte
    Keyword byte is used to declare variables of byte primitive data type. The byte data type is an 8-bit signed two's complement integer.

  • case
    case keyword is used in a switch statement. a switch statement can have many case labels. Switch statement compares a value with each case label and executes the block of code associated with matching case label. If none of the case label matches then it will execute default label(if exists).

  • catch
    catch keyword is used with try block for exception handling. Whenever an exception is thrown from a try block, it is compared with the exceptions of catch block. If the exception matches with one of the catch exception then corresponding code block gets executes. A catch block specifies the actions needs to be taken whenever a exception happens.

  • char
    Keyword char is used to declare variables of char primitive data type. The char data type is a single 16-bit Unicode character. It is used to store one character.

  • class
    class keyword is used for declaration and definition of classes in java.

  • const
    const keyword is used to declare a constant value in Java.

  • continue
    continue keyword in java is used to skips the remainder of the loop's body and continues with the next iteration. Whenever a continue statement is executed inside loop's body all statements of loop's body after continue is skipped.

  • default
    default keyword is used in switch statement to specify a code block to be executed if no case label matches the given value. default code block is optional in switch statement.

  • do
    do keyword is used in do-while loop. do keyword marks the beginning of the body of do-while loop. do-while loop is similar to while loop, except here condition is evaluated after execution of the loop's body.

  • double
    Keyword double is used to declare variables of primitive double data type. The double data type is a double-precision 64-bit IEEE 754 floating point numbers.

  • else
    Keyword else is used with keyword if to create a decision making control statement. If the conditional expression associated with if bloc evaluates to false then else block will gets executed.

  • enum
    enum keyword is used to declare enumerated data types.

  • extends
    Keyword extends is used in definition of classes and interfaces to declare the class or interface that is to be extended. In other words, extends keyword is used in the declaration of sub-classes/interfaces to specify which class/interface they are extending.

  • final
    final keyword is used to define a variable whose value cannot be changed. A final class cannot be cannot be extended. A final method cannot be overridden in sub-classes.

  • finally
    finally keyword is used to define an optional code block in try catch statement. finally code block will execute always irrespective of whether an exception is thrown or not.

  • float
    Keyword float is used to declare variables of float primitive data type. The float data type is a single-precision 32-bit IEEE 754 floating point number.

  • for
    for keyword is used in for loop. It marks the beginning of the for loop, containing initialization, conditional expression and increment steps.

  • goto
    goto keyword is no longer used in Java.

  • if
    If keyword is used in if statement which tests a boolean expression and executes a block of code if boolean statement evaluates to true. it is also used in if-else and if-else ladder statements.

  • implements
    implement keyword is used in a class declaration to specify the interface implemented by the class.

  • import
    import keyword is used at the beginning of the java source file to include the classes or packages used in later part of the code.

  • instanceof
    instanceof is a boolean operator which is used to check if an object reference is an instance of a type. It always returns a boolean value.

  • int
    Keyword int is used to declare integer variables. The int data type is a 32-bit signed two's complement integer.

  • interface
    interface keyword is used in declaration of a java interface. An interface can later be implemented by a class.

  • long
    Keyword long is used to declare variables of long primitive data type. The long data type is a 64-bit two's complement integer.

  • native
    native keyword is used to declare methods which is implemented in different language in some other source file.

  • new
    new keyword is used to create objects of a class.

  • package
    package keyword is used to declare the package and name space of a java file. It should be the first line in java program.

  • private
    private keyword is used in the declaration of member variable, member methods and inner classes of a class. A private field or method is only visible from within the class.

  • protected
    protected keyword is used in the declaration of member variable, member methods and inner classes of a class. A protected member is only accessible from within class, subclasses and class of same package.

  • public
    public keyword is used in the declaration of class, interface, instance variable, methods. a public field or member is accessible by any class.

  • return
    return statement is used to exit from a function and transfer control back to the caller. It is also used to return some value to the caller.

  • short
    Keyword short is used to declare variables of short primitive data type. The short data type is a 16-bit signed two's complement integer.

  • static
    static keyword is used in the declaration of class, methods and fields. Static field and methods can be accessed without creating instance of class.

  • strictfp
    strictfp keyword is used to restrict the precision and rounding of floating point calculations regardless of the underlying operating system and hardware platform.

  • super
    super keyword is used in the methods of sub-classes to call the methods of super class. super is also used to call the constructor method of super class from constructor of sub class.

  • switch
    switch keyword is used in switch case statement. switch statement evaluates a variable and then compares it's value with case labels. If any of the case label matches the it executes corresponding block of code else it execute default block(if present).

  • synchronized
    synchronized keyword is used in the declaration of code block and methods to restrict multiple threads from accessing it simultaneously.

  • this
    this contains the reference of current object. this is used to access the fields and methods of current object.

  • throw
    throw keyword is used to throw an exception.

  • throws
    throws keyword is used in the function declaration to specify that what exceptions this method can throw to caller method.

  • transient
    transient keyword is used in the declaration of instance variables to specify that this field should not be serialized while serializing whole object to stream of bytes. Member variables marked as transient will gets lost during serialization of object for network transfer.

  • try
    try keyword is used in try-catch block for exception handling. try block wraps a set of statements that have exception handling. If any statement inside try block throws an exception then this exception will be compared with each catch block for exception handling.

  • void
    void keyword is used in the declaration of a method to specify that method doesn't return any value.

  • volatile
    volatile keyword is used in the declaration of variable to specify that this variable is modified by multiple concurrent threads asynchronously.

  • while
    while keyword is used in while loop. It is also used in do-while loop.