Java Instanceof Keyword
To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface).
The type of comparison in java differentiates the instance with type that is why instanceof operator in Java is also called as a type of comparison operator. True or false is returned in the program. The variable that has a zero-value assigned to it when the instanceof operator is implemented in returning false value.
Simple example of java instanceof
class II1{
public static void main (String s[]) {
II1 h=new II1();
System.out.println(s instanceof II1) ;//true
}
}
Output:
True
To an object a type of parent class is also implemented of subclass type. For instance, if Dog extends Animal, then either the Dog class or the Animal class may refer to an object of Dog.
Another example of java instanceof operator:
class Aml {}
class D1 extends Aml {/ Animal inherited dog
public static void main (String s[]) {
D1 k=new D1();
System.out.println(k instanceof Aml) ;//true
}
}
Output:
true
Instanceof in java with a variable that have null value
The instanceof operator returns false when used on a variable that has no value. Look at the example below where the instanceof operator is used with a variable that has no value.
class D2{
public static void main (String s []) {
D2 k=null;
System.out.println(k instanceof D2) ;//false
}
}
Output of the given program is:
False
Downcasting with java instanceof operator:
Downcasting occurs when a subclass type refers to an object of the parent class. If we try to execute it immediately, the compiler returns an error. A ClassCastException is thrown at runtime if you typecast it. However, downcasting is doable if we utilise the instanceof operator.
Do h=new Aml () ;//Compilation error
When downcasting is implemented, a ClassCastException is thrown at runtime.
Do h=(Do)new Aml ();
// ClassCastException is thrown at runtimebut execution is succesfull.
Possibility of downcasting with instanceof
Let's see the example, where downcasting is possible by instanceof operator.
class Aml {}
class Do3 extends Aml {
static void method (Aml a) {
if (a instanceof Do3) {
Do3 k=(Do3) a;//downcasting
System.out.println("downcasting is performed in program ");
}
}
public static void main (String [] s) {
Aml a=new Do3();
Do3.method(a);
}
}
Output of the program is:
downcasting is performed in program
Downcasting without the use of java instanceof
As shown in the example below, downcasting can also be done without the instanceof operator:
class Aml {}
class D4 extends Aml {
static void method (Aml a) {
D4 d=(D4) a;//downcasting
System.out.println("downcasting is performed in program ");
}
public static void main (String [] s) {
Aml a=new D4();
D4. method(a);
}
}
Output:
ok downcasting performed
Let's look at this more closely. The actual object to which an refers is an object of the Dog class. So, it's okay if we downcast it. However, what will occur if we write:
Aml a=new Aml ();
Dogee.method(a);
//Now ClassCastException but not in case of instanceof operator
Understanding Real use of instanceof in java
An example using or implmenting the instanceof word is in practise of real example.
interface exe {}
class R implements exe {
public void r() {System.out.println("r method");}
}
class S implements exe {
public void s (){System.out.println("s method");}
}
class Cal {
void wake (exep) {//upcasting
if (p instanceof A) {
Rr=(R)p;//Downcasting
r.r();
}
if (p instanceof S) {
Ss=(S)p;//Downcasting
s.s();
}
}
}//end of Call class
class T4{
public static void main (String s []) {
Printable p=new S ();
Cal J=new Cal ();
J. invoke(p);
}
}
Output:
S method
Instanceof Operator
To determine whether an object belongs to a specific type, we utilise the binary operator instanceof. Either true or false is the outcome of the procedure. It compares the instance with the type, which is why it is also referred to as a type of comparison operator.
It is usually advisable to use the instanceof check before casting an unidentified object. By doing this, a runtime ClassCastException is reduced.
Instanceof Operator Work
The instanceof operator implemented on the rules of is-a relationship. The conclusion concept of an is-a relationship is depended on class of parent and child or interface implementation
We'll build a Shape interface to execute this:
public interface symbol {
// implementation details
}
Additionally building a class of a Circle that implements the round class and implements the Shape interface that is been used.
public class Circle extends Round implements Symbol{// details of implementation of circle}
Test public void givenWhenObjectIsInstanceOfType_thenReturnTrue () {Circle c = new Circle (); Assert.assertTrue (c instanceof Circle);}