Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Java Coding Problems - Second Edition

You're reading from  Java Coding Problems - Second Edition

Product type Book
Published in Mar 2024
Publisher Packt
ISBN-13 9781837633944
Pages 798 pages
Edition 2nd Edition
Languages
Author (1):
Anghel Leonard Anghel Leonard
Profile icon Anghel Leonard

Table of Contents (16) Chapters

Preface Text Blocks, Locales, Numbers, and Math Objects, Immutability, Switch Expressions, and Pattern Matching Working with Date and Time Records and Record Patterns Arrays, Collections, and Data Structures Java I/O: Context-Specific Deserialization Filters Foreign (Function) Memory API Sealed and Hidden Classes Functional Style Programming – Extending APIs Concurrency – Virtual Threads and Structured Concurrency Concurrency ‒ Virtual Threads and Structured Concurrency: Diving Deeper Garbage Collectors and Dynamic CDS Archives Socket API and Simple Web Server Other Books You May Enjoy
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.

You have been reading a chapter from
Java Coding Problems - Second Edition
Published in: Mar 2024 Publisher: Packt ISBN-13: 9781837633944
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}