Photo by Camille Orgel on Unsplash

Reserved words are words that cannot be used as an identifier such as variables, functions. Reserved are majorly used for syntactic definitions. Keywords are closely related and often interchangeably used notions for Reserved words. A keyword is a word with special meaning in a particular context. In most modern languages keywords are a subset of reserved words, and it gives advantages during parsing as keywords cannot be confused with identifiers.

In Java, all keywords are reserved words, but some reserved words are not keywords — these are “reserved for future use”. There are a total of 51 keywords out of which 49 are in use and 2 are reserved for future use.

In this article, we will discuss some lesser-used keywords in Java. A complete list of keywords can be found here. Java keywords can be vaguely divided into the following categories: Primitive types and void, Modifiers, Declarations, Control Flow, Miscellaneous. Please note that in this article we will also be looking into reserved keywords for Java 8. Let’s look into a few of these keywords:

abstract: abstract keyword is used to implement abstraction in Java. An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes can’t be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon).

// abstract class example
public abstract class Person {
// declare fields
// declare nonabstract methods
abstract void details(); // abstract method example
}

Few points to note:

  1. When an abstract class is subclassed, the subclass needs to provide implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

instanceof: instanceof is a binary operator used to test if an object is of a given type. The result of the operation is either true or false. Before casting an unknown object, the instanceof check should always be used. Syntax is: (object) instanceof (type) . The instanceof operator works on the principle of the is-a relationship. is-a relationship works for either class inheritance or interface implementation.

Uses:

public interface Shape {}
public class Round {}
public class Circle extends Round implements Shape {}
// Instance of class Circle
Circle circle = new Circle();
Assert.assertTrue(circle instanceof Circle); // True
Assert.assertTrue(circle instanceof Round); // True
Assert.assertTrue(circle instanceof Shape); // True

Few points to note:

  1. Since every class implicitly inherits from Object class. instanceof operator with type as Object will always evaluate true.

module: module keyword has been introduced in Java from Java 9. A Java Module is a mechanism to package up your Java application and Java packages into Java modules. It comprises of one or more Java packages that belong together. A module can have a full Java application or A Java Platform API or a third-party API.

The major benefit modules are that it allows APIs to be split into separate modules thus making the distributed application footprints smaller. It also helps in the encapsulation of internal packages and also allows the detection of missing modules at startup. For more details on modules check this article by Jakob Jenkov.

Computers are all I want to know about.