Powered by Blogger.

Java | Definition of The instanceof Operator Program in Java

Definition of Instanceof Operator:-

Using the instanceof operator, we can know the actual object of a class. At line 22, we are passing object through argument list of test(Object x) method but we do not know this x object is from which class. Then we use instanceof operator to know object x is from which class by the conditions.

Syntax:-

object instanceof constructor

Parameters:-

1. Object

The object to test.

2. Constructor

Function to test against

Instanceof Operator Program:-

 class Car{
   public void abc(){
     System.out.println(”Car”);
   }
 }

 class Bus{
   public void xyz(){
     System.out.println(”Bus”);
   }
 }

 public class Testinstanceof{
   public static void main(String[]args){
     Car c = new Car();
     Bus b = new Bus();
     Testinstanceof t = new Testinstanceof();
     t.test(c);
     t.test(b);
   }

 public void test (Object obj){
   if(obj instanceof Car){
      System.out.println(”Object is of Car class”);
   }else if(obj instanceof Bus){
      System.out.println(”Object is of Bus class”);
   }else{
      System.out.println(”Object is not of Car/Bus class”);
   }
  }
 }

The Equality and Relational Operators:-

The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand. The majority of these operators will probably look familiar to you as well. Keep in mind that you must use "==", not "=", when testing if two primitive values are equal.

==      equal to
!=      not equal to
>       greater than
>=      greater than or equal to
<       less than
<=      less than or equal to

The Conditional Operators:-

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.

&& Conditional-AND
|| Conditional-OR

The Type Comparison Operator Instanceof:-

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

The following program, InstanceofDemo, defines a parent class (named Parent), a simple interface (named MyInterface), and a child class (named Child) that inherits from the parent and implements the interface.

Instanceof Program:

class InstanceofDemo {
    public static void main(String[] args) {

        Parent obj1 = new Parent();
        Parent obj2 = new Child();

        System.out.println("obj1 instanceof Parent: "
            + (obj1 instanceof Parent));
        System.out.println("obj1 instanceof Child: "
            + (obj1 instanceof Child));
        System.out.println("obj1 instanceof MyInterface: "
            + (obj1 instanceof MyInterface));
        System.out.println("obj2 instanceof Parent: "
            + (obj2 instanceof Parent));
        System.out.println("obj2 instanceof Child: "
            + (obj2 instanceof Child));
        System.out.println("obj2 instanceof MyInterface: "
            + (obj2 instanceof MyInterface));
    }
}

class Parent {

}

class Child extends Parent implements MyInterface {

}

interface MyInterface {
}

Output:-

obj1 instanceof Parent: true
obj1 instanceof Child: false
obj1 instanceof MyInterface: false
obj2 instanceof Parent: true
obj2 instanceof Child: true
obj2 instanceof MyInterface: true

When using the instanceof operator, keep in mind that null is not an instance of anything.

2 comments:

  1. I really enjoy the blog.Much thanks again. Really Great.
    Very informative article post. Really looking forward to read more. Will read on…


    oracle online training
    sap fico online training
    dotnet online training
    qa-qtp-software-testing-training-tutorial

    ReplyDelete
  2. I appreciate you sharing this article. Really thank you! Much obliged.
    This is one awesome blog article. Much thanks again.

    sap online training
    software online training
    sap sd online training
    hadoop online training
    sap-crm-online-training

    ReplyDelete