Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
The instanceof
keyword in Java is a type comparison operator that checks if an object is an instance of a specific class or an interface. It is commonly used for type checking and casting objects. In this tutorial, we'll discuss the basics of the instanceof
keyword in Java and provide examples of its usage.
The instanceof
keyword returns a boolean value, which is true if the object being compared is an instance of the specified class or interface, and false otherwise.
Syntax:
object instanceof ClassName
Example:
public class Main { public static void main(String[] args) { String str = "Hello, World!"; boolean isString = str instanceof String; System.out.println("Is 'str' a String? " + isString); // true } }
In this example, we use the instanceof
keyword to check if the str
object is an instance of the String
class. The result is true because str
is a String object.
The instanceof
keyword can also be used to check if an object is an instance of a class in an inheritance hierarchy.
Example:
class Animal {} class Dog extends Animal {} public class Main { public static void main(String[] args) { Dog dog = new Dog(); boolean isAnimal = dog instanceof Animal; System.out.println("Is 'dog' an Animal? " + isAnimal); // true } }
In this example, we have a Dog
class that extends the Animal
class. We use the instanceof
keyword to check if the dog
object is an instance of the Animal
class. The result is true because Dog
is a subclass of Animal
.
The instanceof
keyword can be used to check if an object implements a specific interface.
Example:
interface Drawable {} class Circle implements Drawable {} public class Main { public static void main(String[] args) { Circle circle = new Circle(); boolean isDrawable = circle instanceof Drawable; System.out.println("Is 'circle' Drawable? " + isDrawable); // true } }
In this example, we have a Circle
class that implements the Drawable
interface. We use the instanceof
keyword to check if the circle
object is an instance of the Drawable
interface. The result is true because Circle
implements Drawable
.
The instanceof
keyword cannot be used with primitive types, as it is only applicable to objects. Additionally, if the object being compared is null
, the instanceof
keyword will return false, since null
is not an instance of any class or interface.
In conclusion, the instanceof
keyword in Java is a powerful tool for checking the type of an object, particularly in the context of inheritance and interfaces. By understanding the basics of the instanceof
keyword and its usage, you can write safer and more robust code.
Example of using instanceof for type checking in Java
class Animal { } class Dog extends Animal { } // Type checking with instanceof Animal animal = new Dog(); if (animal instanceof Dog) { System.out.println("It's a Dog!"); }
Checking class hierarchy with instanceof keyword
class Shape { } class Circle extends Shape { } // Checking class hierarchy with instanceof Shape shape = new Circle(); if (shape instanceof Circle) { System.out.println("It's a Circle!"); }
Using instanceof with interfaces in Java
interface Greetable { } class Person implements Greetable { } // Using instanceof with interfaces Greetable greetable = new Person(); if (greetable instanceof Person) { System.out.println("It's a Person!"); }
Avoiding instanceof and using polymorphism instead in Java
class Animal { void makeSound() { System.out.println("Some sound"); } } class Dog extends Animal { @Override void makeSound() { System.out.println("Woof!"); } } // Using polymorphism instead of instanceof Animal animal = new Dog(); animal.makeSound(); // Calls Dog's makeSound
Java instanceof vs. getClass() for type checking
// Using instanceof if (animal instanceof Dog) { System.out.println("It's a Dog!"); } // Using getClass() if (animal.getClass() == Dog.class) { System.out.println("It's a Dog!"); }
Handling null and instanceof in Java
Animal animal = null; // Handling null with instanceof if (animal instanceof Dog) { System.out.println("It's a Dog!"); } else { System.out.println("It's null"); }