Purpose of ‘instanceof’ keyword in java

Explain the purpose of ‘instanceof’ keyword in java. Provide an example.

The instanceof keyword is used to check the type of an instance of an object.

Example:
String s = "XYZ";
if (s instanceof java.lang.String)
returns TRUE.
But, using instanceof on a null reference variable returns FALSE

Example:
String s = null;
if (s instanceof java.lang.String)
returns FALSE
In a parent child relationship the instanceof would return TRUE.

Example:
if (child instanceof Parent)
returns TRUE
What is cloning? Explain how to design a class that supports cloning
What is cloning? - If you create an object on the heap and assign the object to another object, then the compiler does not create another object...
What are packages in java? Explain the purposes of packages
What are packages in java? - A package is a grouping of related types providing access protection and name space management...
What are access levels for classes and interfaces?
Access levels for classes and interfaces...
Post your comment