Book Image

Java Coding Problems - Second Edition

By : Anghel Leonard
Book Image

Java Coding Problems - Second Edition

By: Anghel Leonard

Overview of this book

The super-fast evolution of the JDK between versions 12 and 21 has made the learning curve of modern Java steeper, and increased the time needed to learn it. This book will make your learning journey quicker and increase your willingness to try Java’s new features by explaining the correct practices and decisions related to complexity, performance, readability, and more. Java Coding Problems takes you through Java’s latest features but doesn’t always advocate the use of new solutions — instead, it focuses on revealing the trade-offs involved in deciding what the best solution is for a certain problem. There are more than two hundred brand new and carefully selected problems in this second edition, chosen to highlight and cover the core everyday challenges of a Java programmer. Apart from providing a comprehensive compendium of problem solutions based on real-world examples, this book will also give you the confidence to answer questions relating to matching particular streams and methods to various problems. By the end of this book you will have gained a strong understanding of Java’s new features and have the confidence to develop and choose the right solutions to your problems.
Table of Contents (16 chapters)
1
Text Blocks, Locales, Numbers, and Math
Free Chapter
2
Objects, Immutability, Switch Expressions, and Pattern Matching
14
Other Books You May Enjoy
15
Index

56. Hooking instanceof in a nutshell

Having an object (o) and a type (t), we can use the instanceof operator to test if o is of type t by writing o instanceof t. This is a boolean operator that is very useful to ensure the success of a subsequent casting operation. For instance, check the following:

interface Furniture {};
class Plywood {};
class Wardrobe extends Plywood implements Furniture {};

instanceof returns true if we test the object (for instance, Wardrobe) against the type itself:

Wardrobe wardrobe = new Wardrobe();
if(wardrobe instanceof Wardrobe) { } // true
Plywood plywood = new Plywood();
if(plywood instanceof Plywood) { } // true

instanceof returns true if the tested object (for instance, Wardrobe) is an instance of a subclass of the type (for instance Plywood):

Wardrobe wardrobe = new Wardrobe();
if(wardrobe instanceof Plywood) {} // true

instanceof returns true if the tested object (for instance, Wardrobe) implements the interface represented by the type (for instance, Furniture):

Wardrobe wardrobe = new Wardrobe();
if(wardrobe instanceof Furniture) {} // true

Based on this, consider the following note:

Important note

The logic behind instanceof relies on the IS-A relationship (this is detailed in The Complete Coding Interview Guide in Java, Chapter 6, What is inheritance?). In a nutshell, this relationship is based on interface implementation or class inheritance. For instance, wardrobe instanceof Plywood returns true because Wardrobe extends Plywood, so Wardrobe IS A Plywood. Similarly, Wardrobe IS A Furniture. On the other hand, Plywood IS-not-A Furniture, so plywood instanceof Furniture returns false. In this context, since every Java class extends Object, we know that foo instanceof Object returns true as long as foo is an instance of a Java class. In addition, null instanceof Object (or any other object) returns false, so this operator doesn’t require an explicit null check.

Finally, keep in mind that instanceof works only with reified types (reified type information is available at runtime), which include:

  • Primitive types (int, float)
  • Raw types (List, Set)
  • Non-generic classes/interfaces (String)
  • Generic types with unbounded wildcards (List<?>, Map<?, ?>)
  • Arrays of reifiable types (String[], Map<?, ?>[], Set<?>[])

This means that we cannot use the instanceof operator (or casts) with parameterized types because the type erasures alter all type parameters in generic code, so we cannot say which parameterized type for a generic type is in use at runtime.