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