JavaBeginner#fundamentals

What is the instanceof operator?

It checks whether an object is an instance of a specific class or implements a given interface, returning a boolean, and is often used before casting to avoid ClassCastException.

Example
Object obj = "hello";
if (obj instanceof String s) { // pattern matching (Java 16+)
  System.out.println(s.length());
}

Related Questions

1
JavaBeginner#fundamentals#java10

What is the difference between var and explicit typing in Java 10+?

Open
2
JavaIntermediate#inner-classes

What is the difference between a static nested class and a non-static inner class?

Open
3
JavaIntermediate#inner-classes

What are anonymous inner classes used for?

Open