instanceof Operator – Java

instanceof operator

The instanceof operator allows you to check the validity of a IS A relationship. If at any point of time, we are not sure about this and we want to validate this at runtime, we can do the following:

//assuming vehicle is an instance of Class `Car` the expression inside the 'if' will return true if(vehicle instanceof Car){ //do something if vehicle is a Car }

Note: If you apply the instanceof operator with any variable that has null value, it returns false.

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.