Quirky keywords in Java

Prashant Shubham
4 min readDec 26, 2019
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.
  2. In the case of interface, all the methods declared are implicitly abstract if not declared default or static.
  3. An abstract class may have static fields and static methods.
  4. An abstract class can implement an interface without implementing all the methods of the interface.
  5. An example of an abstract class in the JDK is AbstractMap, which is part of the Collections Framework. Subclasses for this class are: HashMap, TreeMap and ConcurrentHashMap .

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.
  2. When using instanceof the null check is not required to be done explicitly. object with null values will always return false.
  3. When overriding equals method in Java it is advised to not use instanceof check unless the class is immutable i.e can’t have subclasses, as instanceof check returns true even for the child class. Instead of instanceofwe can use getClass()method for type identification.

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.

--

--