Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Thursday 1 June 2017

Java Instanceof

 Java Instanceof

Java instanceof

In java, instanceof is a binary operator, Which is used to test whether the object is an instance of the specified type(class or subclass or interface) or not.

If the object is an instance of a particular or specified type i.e class or subclass or interface, it returns true otherwise it returns false.

The java instanceof operator is also known as type comparison operator because it compares the instance with type.


Java Instanceof Example

There is single class Test and we will instance with type.

class Test
{
public static void main(String args[])
{
Test t = new Test();
System.out.println(t instanceof Test);//will print true
}


output : true

In the above example , The output is true because t is a type of Test class.


Java Instanceof Example 2

Java instanceof operator in case of inheritance i.e parent child relationship. An object of a child class type is also a type of its parent class.

class Vehicle
{}
class Bike extends Vehicle
{
public static void main(String args[])
{
Bike b = new Bike();
System.out.println("Bike is a type of Vehicle");
System.out.println(b instanceof Vehicle);//will print true
}
}

output : Bike is a type of Vehicle
              true

In the above example, The output is true because Bike extends Vehicle hence the object of Bike(child class) can be referred by either Bike or Vehicle class.


Java Instanceof Example 3

Another example of instanceof operator in java, In this example we will apply instanceof operator with a variable that have null value. If we apply instanceof with a variable that have null value then it will return false.

class Employee
{
public static void main(String args[])
{
Employee e = null;
System.out.println(e instanceof Employee);
}
}

output : false



Java Downcasting With Instanceof Operator

Now, There is a word downcasting in java.

What is Downcasting in java ?

Downcasting in java , When child class type refers to the object of the parent class is called downcasting in java.

If we perform downcasting directly it will give compile time or run time errors in java. Let's take a look 

Child c = new Parent();//compile time error

Child c =(Child)new Parent();//ClassCastException at run-time

But by using instanceof operator we can easily perform downcasting in java.


Java Downcasting Example

In this example, we will perform java downcasting with the help of instanceof operator in java.

class Vehicle
{}
class Bike extends Vehicle
{
public static void check(Vehicle v)
{
if(v instanceof Bike)
{
Bike b = (Bike)v;//downcasting perform
System.out.println("Downcasting perform");
}
}
public static void main(String args[])
{
Vehicle v = new Bike();
Bike.check(v);
}
}



output : Downcasting perform


Java Downcasting Example 2

In this example We will perform downcasting without the help of instanceof operator.


class Vehicle

{}

class Bike extends Vehicle

{

public static void check(Vehicle v)

{

Bike b = (Bike)v;//downcasting perform

System.out.println("Downcasting perform");

}

public static void main(String args[])

{

Vehicle v = new Bike();

Bike.check(v);

}

}

output : Downcasting perform


Now, Here we learned in detail about instanceof operator and what is downcasting , downcasting with instanceof and without instanceof.


Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate