The instanceof operator in Java is an operator used to check if this object is an instance of a certain class or interface or not? The return result of this operator will be true if the object is an instance of the class you are checking, otherwise false.
For example, I have an Application class as follows:
1 2 3 4 5 6 7 8 9 10 |
package com.huongdanjava.java; public class Application { public static void main(String[] args) { Application application = new Application(); System.out.println(application instanceof Application); } } |
In the main() method of this class, I initialize an object of class Application and use the instanceof operator to check if this object is an instance of this Application class or not? You will see the following results:
If you write the following code:
1 2 3 4 5 6 7 8 9 10 |
package com.huongdanjava.java; public class Application { public static void main(String[] args) { Application application = new Application(); System.out.println(application instanceof String); } } |
then IDE will have an error:
This is in the case of too obvious, the IDE can report the error to you immediately, but if you have an interface with two implementations as follows:
1 2 3 4 5 |
package com.huongdanjava.java; public interface Shape { } |
1 2 3 4 5 |
package com.huongdanjava.java; public class Triangle implements Shape { } |
1 2 3 4 5 |
package com.huongdanjava.java; public class Rectangle implements Shape { } |
then now if you initialize the object of Triangle class but check if this object is an instance of class Rectangle:
1 2 3 4 5 6 7 8 9 10 11 |
package com.huongdanjava.java; public class Application { public static void main(String[] args) { Shape shape = new Triangle(); System.out.println(shape instanceof Rectangle); } } |
The IDE will not be able to detect errors at compile time, but when running, you will see the following results:
We will often use the instanceof operator in case of checking whether a method’s parameter is an instance of a certain class. Eg:
1 2 3 4 5 6 7 |
private void check(Shape shape) { if (shape instanceof Triangle) { Triangle triangle = (Triangle) shape; System.out.println("This is triangle: " + triangle.toString()); } } |
In the above method, the interface Shape parameter has many different implementations, in the body of the method, we will check if the instance passed to this method is Triangle or not? If it is correct, then continue to process.
Result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.huongdanjava.java; public class Application { private void check(Shape shape) { if (shape instanceof Triangle) { Triangle triangle = (Triangle) shape; System.out.println("This is triangle: " + triangle.toString()); } } public static void main(String[] args) { Application application = new Application(); application.check(new Triangle()); } } |
From Java 14, you can rewrite the check() method using pattern matching instanceof, as simple as:
1 2 3 4 5 |
private void check(Shape shape) { if (shape instanceof Triangle triangle) { System.out.println("This is triangle: " + triangle.toString()); } } |
With the new way, we don’t need to write a single line of code to cast the instance of the object we want. All will be done in the if block, the result remains the same: