HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java Object Oriented Programming / Java instanceof operator

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?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. mr developer

    February 3, 2020

    I need to cast an Object to ArrayList.
    I did your 4th solution but im still getting a warning.
    why?

Comments are closed on this article!

Search Tutorials

Java OOP

  • OOP – Introduction
  • OOP – Access Modifiers
  • OOP – Constructors
  • OOP – Instance Initializers
  • OOP – Abstraction
  • OOP – Encapsulation
  • OOP – Inheritance
  • OOP – Polymorphism
  • OOP – Overloading vs Overriding
  • OOP – Interface vs Abstract Class
  • OOP – extends vs implements
  • OOP – instanceof operator
  • OOP – Multiple Inheritance
  • Association, Aggregation and Composition

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)