0% found this document useful (0 votes)
15 views6 pages

Java Instanceof Operator

The Java instanceof operator is used to determine if an object is an instance of a specified class, subclass, or interface, returning a boolean value. It allows for type comparison and is essential for downcasting objects safely. The document provides examples of using instanceof for checking object types, handling null values, and performing downcasting in Java.

Uploaded by

maitreyee658
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Java Instanceof Operator

The Java instanceof operator is used to determine if an object is an instance of a specified class, subclass, or interface, returning a boolean value. It allows for type comparison and is essential for downcasting objects safely. The document provides examples of using instanceof for checking object types, handling null values, and performing downcasting in Java.

Uploaded by

maitreyee658
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java instanceof operator

The Java instanceof Operator is used to determining whether this object


belongs to this (class or subclass or interface) or not.

The java 'instanceof' operator is used to test whether an object is an instance of a


specified type (class or sub - class or visual interface).

'instanceof' in Java is also known as the type comparison operator because it


compares the instances with type. It returns true or false. If we apply the
'instanceof' operator with a variable value, the value returned is false.

That means, this operator gives the boolean values such as true or false. If it
relates to a specific class, then it returns true as output. Otherwise, it returns false
as output.

Syntax:
object-reference instanceof type;

Example of Java instanceof Operator


Let's look at a simple example of instanceof operator where it checks the current
class.

student.java
{
public static void main( String args[ ] )
{
// declaring an object 's' of the student class
student s = new student( ) ;
// checking whether s is an instance of the student class using instanceof
operator
Boolean str = s instanceof student;
// printing the string value
System.out.println( str ) ;
}
}
Output:
true

An object of subclass type is also a type of parent class. For example, if a 'student'
class extends the 'Teacher' class, then object of student class can be referred by
either the student class itself or Teacher class. Let us consider the following
example to understand this point more clearly.
Let's see another example.

Student.java

class Teacher { }
public class Student extends Teacher
{
public static void main( String args[ ] )
{
// declaring the object of the class 'Student'
Student s = new Student( ) ;
// checking whether the object s is the instance of the parent class ' Teacher '
Boolean str = s instanceof Teacher ;
// printing the boolean value
System.out.println( str ) ;
}
}
Output:
true

Using instanceof Operator with a Variable that has Null Value


Let's take some time here to think of the outcome if the instanceof operator is
used with a variable that has null value. The answer is that it returns false.
Consider the following example to gain better understanding of this point.

Student.java

public class Student


{
public static void main( String args[ ] )
{
// initializing the object with null value
Student s = null ;

// checking whether the s object with null value is the instance of the Student
class
Boolean str = s instanceof Student ;

// printing the boolean value


System.out.println( str ) ;
}
}
Output:
false

Downcasting with Java instanceof Operator


In Downcasting the object of the parent class is assigned to the base class. On
performing it directly, the compiler gives Compilation error. On the other hand,
if we try to perform it by typecasting, ClassCastException is thrown at runtime.
Downcasting is easily possible using 'instanceof' operator.

Student s = new Teacher( ) ; // Compilation error


If we perform downcasting by typecasting, ClassCastException is thrown at
runtime.

Student s = (Student) new Teacher( ) ;


Compiles successfully but ClassCastException is thrown at runtime

Possibility of downcasting with instanceof Operator


Let's see the example, where downcasting is possible by instanceof operator.

Student.java

class Teacher { }
public class Student extends Teacher {
static void method( Teacher T )
{
// checking if T (instance of Teacher class) is the instance of Student class
if( T instanceof Student )
{
// performing downcasting
Student s = ( Student ) T ;
System.out.println( " Cool! Downcasting successfully performed! " ) ;
}
}
public static void main ( String [] args )
{
// crating object of class 'Teacher'
Teacher T = new Student( ) ;

Student.method( T ) ;
}
}
Output:
Cool! Downcasting successfully performed!

Program to Show Downcasting with instanceof Operator


class Company {}

public class Employee extends Company {


public void check() {
System.out.println("Success.");
}

public static void view(Company c) {


if (c instanceof Employee) {
Employee b1 = (Employee) c;
b1.check();
}
}

public static void main(String[] args) {


Company c = new Employee();
Employee.view(c);
}
}
Output:
Success.

Downcasting without the use of Java instanceof Operator


Let's see how we can perform downcasting without using the instanceof operator
as shown in the following example:

Student.java

class Teacher { }
public class Student extends Teacher
{
static void method( Teacher T )
{
// performing downcasting
Student s = ( Student ) T ;
System.out.println( " Cool! Downcasting successfully performed! " ) ;
}

public static void main ( String [ ] args )


{
Teacher T = new Student( ) ;
Student.method( T ) ;
}
}
Output:
Cool! Downcasting successfully performed!

Understanding Real use of instanceof in java


Consider the example given below to understand what we have covered so far.

Temp1.java

// declaring an interface. An interface is a class that has no implementation of its


own and is used as a parent class to be derived by the child class
interface School { }
// class Student implementing School
class Student implements School
{
public void student( )
{
System.out.println( " This is the method of Student class " ) ;
}
}
// class Teacher implementing School
class Teacher implements School
{
public void teacher( )
{
System.out.println(" This is the method of Teacher class " ) ;
}
}
class Temp
{
void call( School sc )
{
// performing upcasting
if( sc instanceof Student )
{
// performing downcasting
Student s = ( Student ) sc ;
// calling the method student of 'Student' class
s.student( ) ;
}
if( sc instanceof Teacher )
{
// performing downcasting
Teacher t = ( Teacher )sc ;
// calling the method teacher of 'Teacher' class
t.teacher( ) ;
}
}
}
public class Temp1
{
public static void main(String[] args)
{
School sc = new Teacher( ) ;
// creating object of temp class
Temp t = new Temp();
t.call( sc ) ;
}
}
Output:
This is the method of Teacher class

You might also like