Java

Java instanceof Operator

The instanceof operator checks if its operand on the left is an object of its operand on the right. If yes, it returns true, otherwise it returns false, or issues an error message at compile time. The operand on the left should be an instantiated object of the operand on the right. The operand on the right is a type, e.g. a class. The syntax of use is:

objectName instanceof typeName

There is a space and no dot between objectName and instanceof. There are no parentheses around the second operand; there is a space and no dot between instanceof and typeName. instanceof is a relational operator in Java. In Java, relational operators are <, >, <=, >=, and instanceof . == and != are not relational operators in Java; they are equality operators. The instanceof operator can be used in the if-condition, as well as in the other relational operators.

There are two kinds of types in Java: primitive types and reference types. Primitive types are not used with the instanceof operator. There are four kinds of reference types which are the class types, interface types, type variables, and array types. The instanceof operator works with these four kinds of reference types. Primitive types are the exceptions to be used with the instanceof operator. This article illustrates the use of the instanceof operator.

Article Content

Primitive and Primitive Wrapper Classes

int is a primitive type. Integer is the wrapper class of the int primitive type. The output of the following program is true:

    public class TheClass {
        public static void main(String[] args) {
            Integer obj = 5;
boolean bl = obj instanceofInteger;
System.out.println(bl);
        }
    }

Integer, beginning with an uppercase of I, is a class reference, while int is a primitive type. The statement that uses the instanceof operator here is the second statement of the main() method. There has to be a space between obj and instanceof and between instanceof and the type.

In the following program, where int is used instead of Integer, a compiler error is issued:

    public class TheClass {
        public static void main(String[] args) {
            int obj = 5;
boolean bl = obj instanceofint;
System.out.println(bl);
        }
    }

The first statement of the main() method, should have begun with Integer instead of int. The second operand of instanceof, should have been Integer, also.

Other primitive types and their corresponding wrapper classes are related in the same way.

Predefined Types

The String type is an example of a predefined type. The output of the following program is true:

public class TheClass {
        public static void main(String[] args) {
            String obj = new String("text");
boolean bl = obj instanceofString;
System.out.println(bl);
        }
    }

Note the way the first and second statements of the main() method have been coded.

Class Reference

The following program has a class reference, AClass. This works with the instanceof operator. The output of the program is true:

    class AClass {}

    public class TheClass {
        public static void main(String[] args) {
AClass obj = new AClass();
boolean bl = obj instanceofAClass;
System.out.println(bl);
        }
    }

Note the way the first and second statements of the main() method have been coded.

Array Types

The array object is indirectly instantiated from the array type. The following program outputs true:

public class TheClass {
        public static void main(String[] args) {
char[] obj = new char[]{'A', 'B', 'C'};
boolean bl = obj instanceofchar[];
System.out.println(bl);
        }
    }

Note the way the first and second statements of the main() method have been coded. The array type is indicated with square brackets, next to the (primitive) type.

Abstract and Interface Types

An abstract method is typically a method signature, beginning with the reserved word, abstract, and ending with a semicolon.

Difference Between Abstract and Interface Classes

An abstract class is preceded by the reserved words, abstract and class. In interface is not preceded by these two words; it is preceded by just interface. An abstract method in an abstract class must be preceded by the reserved word, abstract. An abstract method in an interface class should not be preceded by the reserved word, abstract. A class extends an abstract class, while a class implements an interface.

Sub-Class and Abstract Type

In the following program, Sub is a sub-class (inherited) of the abstract class, Abst. The output confirms that the object, obj of Sub, is an instance of Sub, as well as it is an instance of Abst.

abstract class Abst {
        abstract void abstMthd();
    }

    class Sub extends Abst {
            void abstMthd() {
System.out.println();
        }
    }

    public class TheClass {
        public static void main(String[] args) {
            Sub obj = new Sub();
boolean bl = obj instanceofSub;
booleanbla = obj instanceofAbst;
System.out.println(bl);
System.out.println(bla);
        }
    }

The output is:

true

true

Implementation and Interface Type

In the following program, Impl is an implementation of the interface, Inter. The output confirms that the object, obj of Impl, is an instance of Impl, as well as it is an instance of Inter.

interface Inter {
        void abstMthd();
    }

    class Impl implements Inter {
            public void abstMthd(){
System.out.println();
        }
    }

    public class TheClass {
        public static void main(String[] args) {
Impl obj = new Impl();
boolean bl = obj instanceofImpl;
booleanbli = obj instanceofInter;
System.out.println(bl);
System.out.println(bli);
        }
    }

The output is:

true

true

Type Variable

Type variable is codded in angle brackets. Consider the following program:

import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            Vector obj = new Vector();
boolean bl = obj instanceofVector;
System.out.println(bl);
        }
    }

The output is: true. Note the way the vector coding has been done.

Conclusion

The instanceof operator checks if its operand on the left is an object of its operand on the right. If yes, it returns true, otherwise it returns false, or gives an error message at compile time. The operand on the left should be an instantiated object of the operand on the right. The operand on the right is a type, e.g. a class. instanceof is one of the relational operators in Java. The instanceof operator can be used in the if-condition.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.