Quirky keywords in Java
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).
|
|
Few points to note:
- 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
. - In the case of interface, all the methods declared are implicitly abstract if not declared default or static.
- An abstract class may have
static
fields andstatic
methods. - An abstract class can implement an interface without implementing all the methods of the interface.
- An example of an abstract class in the JDK is
[AbstractMap](https://docs.oracle.com/javase/8/docs/api/java/util/AbstractMap.html)
, 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:
|
|
Few points to note:
- Since every class implicitly inherits from Object class.
instanceof
operator with type asObject
will always evaluate true. - When using
instanceof
the null check is not required to be done explicitly.object
with null values will always return false. - When overriding
equals
method in Java it is advised to not useinstanceof
check unless the class is immutable i.e can’t have subclasses, asinstanceof
check returns true even for the child class. Instead ofinstanceof
we can usegetClass()
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.
I hope this article was informative. In the next part of the article we will look into other keywords: native, static, strictfp, synchronized, transient, _(underscore), volatile.
References: