Java : Keywords with Categories


Java programming language is having 57 reserved words that’s called as keyword. These keyword can not be used as name for variables, methods, classes, interfaces or as any other identifier.

Java Keywords 

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

Note : Out of these 57 keywords only 55 are used by programmers other 2 (const and go) are not used.

Primitive Type Keyword

Primitive types are most basic data types available in Java language which are having predefined fixed size, range , default values and no methods. These types serve as  building blocks of data manipulation in java language.

Primitive Type: boolean, byte, char, short, int, long, float and double.

See Also:

Literal Value Keywords

A literal is the source code representation of a fixed value and assigned directly to variable without requiring computation.

Literal assigned to primitive type variable as it’s not required new keyword. Primitive types are special data types built into the language; they are not objects created from a class. Below are some literal values for Boolean type (true and false) and Non primitive objects (null).

  • true : A boolean literal value.
  • false : A Boolean literal value.
  • null : A reference literal value.

See Also:

Control Flow Keywords

Generally statements in java code executed from top to bottom, in the same order as they appears. Control flow statements change or break the flow of execution by implementing decision making, looping and branching of program to execute particular blocks of code based on certain conditions.

Control Flow statement categories as below:

  • Decision-making statements : if-then, if-then-else, switch-case
  • Looping statements : for, while, do-while
  • Branching statements : break, continue, return

See also :

  • Control Flow Statements with examples

Exception Handling keywords

Keyword Description
try The try block governs the statements that are enclosed within it and defines the scope of exception handler associated with it. Try block follows catch or finally or both.
catch The catch block is used to handle the exception. It must be preceded by try block which means we can’t use catch block alone. It can be followed by finally block later.
finally When an exception is raised, the statement in the try block is ignored, some times it is necessary to process certain statements irrespective of wheather an exception is raised or not, the finally block is used for this purpose.
throw The throw class is used to call exception explicitly. You may want to throw an exception when the user enters a wrong login ID and password, you can use throw statement to do so. The throw statement takes an single argument, which is an Object of exception class.
throws The throws statement species the list of exception that has thrown by a method. If a method is capable of raising an exception that is does not handle, it must specify the exception has to be handle by the calling method, this is done by using the throw statement.

See Also :

Access Modifiers

Java access modifiers helps to restrict the scope of a class, constructor , variable , method or data member. There are four types of access modifiers available in java:

  • default (No Keyword Required)
  • private
  • protected
  • public

Below are the scope of each access modifiers:

default
private protected public
Same Class Yes Yes Yes Yes
Same Package Subclass Yes No Yes Yes
Same Package non Sub Class Yes No Yes Yes
Different Package Sub Class No No Yes Yes
Different Package Non Sub Class No No No Yes

See Also :

Non Access Modifiers

Below are some keywords that use as non-access modifiers to achieve many other functionalities:

Modifiers Description
static Used to declare a field, method, or inner class as a class field.

  • static with class fields:
    Classes maintain one copy of static fields regardless of how many instances exist of that class. These static fields also called as class variables.
  • static with method:
    These methods are bound to the class instead of to a specific instance, and can only operate on class variables. These static method also called as class methods.

Class variables and methods can be accessed using the class name followed by a dot and the name of the variable or method.

final Used to declare an entity that can not be changed or nor derived later.

  • final with variable :
    • A final variable can explicitly assigned only once.
    • If a reference variable declared final can never be reassigned to refer to an different object. However, the data within the object can be changed.
    • A final modifier often is used with static to make the constant a class variable.
  • final with method :
    A final method can not be override.
  • final with class :
    A final class can not be sub classed. All methods in a final class are implicitly final.
abstract A abstract keyword is used with methods and classes to provide abstraction in Java.

  • abstract with methods :
    A method with no definition must be declared as abstract and the class containing it must be abstract.
  • abstract with class :
    • An abstract class can not be instantiated. it must be implemented in sub classes.
    • An abstract class not required to have a abstract method.
    • An abstract class may contain both abstract methods as well normal methods.A class cannot be both abstract and final (since a final class cannot be extended)

    Note : abstract keyword can not be used with variables and constructors.

synchronized The synchronized keyword used to declare a method or code block to acquire mutex lock for an object so that only one thread access at a time. The mutex lock is automatically released when execution exits the synchronized code.

  • The synchronized modifier can be applied with any of the four access level modifiers.
  • For static methods, the object locked is the class’s Class.
  • Fields, classes and interfaces cannot be declared as synchronized.
transient An instance variable is marked transient to indicate the JVM to skip the particular variable when serializing the object containing it. When object deserialized transient values will initialize with default value.
volatile A volatile keyword used in field declarations to specify that the variable is modified asynchronously by concurrently running threads. Volatile variable cached in main memory so that synchronizes value between threads.

  • Volatile can only be applied to instance variables, which are of type object or private.
  • A volatile object reference can be null.
  • This modifier is included in the statement that creates the variable, preceding the class or data type of the variable.
  • Methods, classes and interfaces can not be  declared volatile, nor can local variables or parameters.
strictfp java strictfp keyword is used to restricting floating-point and round of calculations (float and double) and ensuring same result on every platform while performing operations with floating point variable.

See Also: strictfp modifier uses and example

Class , objects and methods related keywords

package

Java package is a group of similar classes and interfaces that used as namespace to resolve conflict between same name class. Packages are declared with the package keyword.

class

A class is template that defines the implementation of a particular kind of object.

  • A class definition defines instance and class fields, methods, and inner classes as well as specifying the interfaces the class implements and the immediate super class of the class.
  • If the super class is not explicitly specified, the super class is implicitly Object.
  • The class keyword can also be used in the form Class.class to get a Class object without needing an instance of that class. For example, String.class can be used instead of doing new String().getClass().

interface

An interface is used to declare a special type of class that provide abstraction.

  • An interface 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.

import

Used at the beginning of a source file to specify classes or entire Java packages to be referred to later without including their package names in the reference.

  • import statements can import static members of a class. (java 5+)

extends

  • Used in a class declaration to specify the super class.
  • Used in an interface declaration to specify one or more super interfaces.
  • Class X extends class Y to add functionality, either by adding fields or methods to class Y, or by overriding methods of class Y. Class X is said to be a subclass of class Y.
  • An interface Z extends one or more interfaces by adding methods. Interface Z is said to be a sub interface of the interfaces it extends.
  • Also used to specify an upper bound on a type parameter in Generics.

implements

Included in a class declaration to specify one or more interfaces that are implemented by the current class. A class inherits the types and abstract methods declared by the interfaces.

new

  • Used to create an instance of a class or array object.
  • It is used to say completely new object need to create. (String Pool)

super

Inheritance basically used to achieve dynamic binding or run-time polymorphism or re-usability in java.

  • Used to access members of a class inherited by the class in which it appears.
  • Allows a subclass to access overridden methods and hidden members of its super class.
  • The super keyword is also used to forward a call from a constructor to a constructor in the super class.
  • Also used to specify a lower bound on a type parameter in Generics.

this

Used to represent an instance of the class in which it appears.

  • The this can be used to access class members and as a reference to the current instance.
  • The this keyword is also used to forward a call from one constructor in a class to another constructor in the same class

return

Used to finish the execution of a method. It can be followed by a value required by the method definition that is returned to the caller.

void

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

instanceof

A binary operator that takes an object reference as its first operand and a class or interface as its second operand and produces a boolean result. The instanceof operator evaluates to true if and only if the runtime type of the object is assignment compatible with the class or interface.

native

Used in method declarations to specify that the method is not implemented in the same Java source file, but rather in another language.[7]

Others Keyword

enum

A Java keyword used to declare an enumerated type. Enumerations extend the base class Enum. This keyword available in java 5  and later.

See Also:

assert

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. This keyword available in java 4  and later.

module

The module keyword is used to declare a module inside of a Java application. This keyword is only available in Java 9 and later.

requires

Used to specify the required libraries inside of a module. This keyword is only available in Java 9 and later.

exports

Used in modular java to export a package with a module. This keyword is only available in Java 9 and later.

References

https://en.wikipedia.org/wiki/List_of_Java_keywords

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s