Java instanceof operator

Java instanceof operator (also called type comparison operator) is used to test whether the object is an instance of the specified type (class or subclass or interface).

It returns –

  • true – if variable is instance of specified class, it’s parent class or implement specified interface or it’s parent interface
  • false – if variable is not instance of the class or implement the interface; or variable is null

1. Java instanceof syntax

instanceof operator tests variable to specified type. Variable is written on left hand side of operator, and type is given on right side of operator.

//<object-reference> instanceof TypeName

boolean value = var instanceof ClassType;

//or

if(var instanceof ClassType) {
	//perform some action
}

2. Java instanceof example

Let’s see an example to fully understand the usage of instanceof operator to compare types. In this example, we are using ArrayList class to test it’s type information.

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class Main 
{
	public static void main(String[] args) 
	{
		ArrayList<String> arrayList = new ArrayList<>();
		
		System.out.println(arrayList instanceof ArrayList);		//true
		
		System.out.println(arrayList instanceof AbstractList);	//true
		
		System.out.println(arrayList instanceof List);			//true
		
		System.out.println(arrayList instanceof Collection);	//true
		
		System.out.println(null instanceof ArrayList);			//false
		
		//System.out.println(arrayList instanceof LinkedList);	//Does not compile
	}
}

Program output.

true
true
true
true
false

3. Java instanceof with arrays

In Java, arrays are also considered objects and have fields and methods associated with them. So we can use instanceof operator with arrays as well.

  • Primitive arrays are instance of Object and self type. e.g. int[] is type of Object and int[]. Both comparison returns true.
  • Object arrays are types of Object, Object array, classtype array, parent class type array. e.g. Integer[] is type of Object, Object[], Integer[] and Number[] (Integer extends Number).
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class Main 
{
	public static void main(String[] args) 
	{
		int[] intArr = new int[3];
		float[] floatArr = new float[3];
		
		Integer[] intObjArr = new Integer[3];
		Float[] floatObjArr = new Float[3];
		String[] stringArr = new String[3];
		
		System.out.println(intArr instanceof Object);		//true
		System.out.println(intArr instanceof int[]);		//true
		
		System.out.println(floatArr instanceof Object);		//true
		System.out.println(floatArr instanceof float[]);	//true
		
		System.out.println(intObjArr instanceof Object);	//true
		System.out.println(intObjArr instanceof Object[]);	//true
		System.out.println(intObjArr instanceof Integer[]);	//true
		System.out.println(intObjArr instanceof Number[]);	//true
		
		System.out.println(floatObjArr instanceof Float[]);	//true
		System.out.println(stringArr instanceof String[]);	//true
	}
}

Program output.

true
true
true
true
true
true
true
true
true
true

4. Using instanceof to correctly typecast

A real life example to use instanceof operator can be typecasting a variable to another type. instanceof operator helps in avoiding ClassCastException in runtime.

Consider following example where we are trying to typecast a list to LinkedList class, where original variable is of type ArrayList. It will throw ClassCastException.

List<String> list = new ArrayList<>();
		
LinkedList<String> linkedList = (LinkedList<String>) list;

To correctly casting the variable, we can use instanceof operator. It will not result in ClassCastException.

List<String> list = new ArrayList<>();
		
if(list instanceof LinkedList) 
{
	LinkedList<String> linkedList = (LinkedList<String>) list;
	
	//application code
}

Drop me your questions related to Java instanceof operator used for type comparison.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “Java instanceof operator”

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.