Use of instanceof in Java
Basically, you check if an object is an instance of a specific class.
You normally use it, when you have a reference or parameter to an object that is of a super class or interface type and need to know whether the actual object has some other type (normally more concrete).
Example:
public void doSomething(Number param) {
if( param instanceof Double) {
System.out.println("param is a Double");
}
else if( param instanceof Integer) {
System.out.println("param is an Integer");
}
if( param instanceof Comparable) {
//subclasses of Number like Double etc. implement Comparable
//other subclasses might not -> you could pass Number instances that don't implement that interface
System.out.println("param is comparable");
}
}
Note that if you have to use that operator very often it is generally a hint that your design has some flaws. So in a well designed application you should have to use that operator as little as possible (of course there are exceptions to that general rule).
Is it good practice to often use instanceof?
Let's say I am writing some inventory code:
public void showInventory(List<Item> items) {
for (Item item : items) {
if (item instanceof ContainerItem) {
// container display logic here
}
else if (item instanceof WeaponItem) {
// weapon display logic here
}
// etc etc
}
}
That will compile and work just fine. But it misses out on a key idea of object oriented design: You can define parent classes to do general useful things, and have child classes fill in specific, important details.
Alternate approach to above:
abstract class Item {
// insert methods that act exactly the same for all items here
// now define one that subclasses must fill in themselves
public abstract void show()
}
class ContainerItem extends Item {
@Override public void show() {
// container display logic here instead
}
}
class WeaponItem extends Item {
@Override public void show() {
// weapon display logic here instead
}
}
Now we have one place to look, the show()
method, in all our child classes for inventory display logic. How do we access it? Easy!
public void showInventory(List<Item> items) {
for (Item item : items) {
item.show();
}
}
We are keeping all the item-specific logic inside specific Item subclasses. This makes your codebase easier to maintain and extend. It reduces the cognitive strain of the long for-each loop in the first code sample. And it readies show()
to be reusable in places you haven't even designed yet.
How to use instanceof operator of java here?
This code is adding Integer
instances followed by String
instances to the same ArrayList
.
Though you didn't say so, it seems the goal of func
is to return an Iterator
that points to the first String
element following the "###" element. Therefore func
should iterate over the elements of the ArrayList
until it encounters a String
instance, which would be the "###" String
.
static Iterator func(ArrayList mylist)
{
Iterator it=mylist.iterator();
while(it.hasNext())
{
Object element = it.next();
if(element instanceof String)
break;
}
return it;
}
How instanceof will work on an interface
First of all, we can store instances
of classes that implements a particular interface
in an interface reference variable
like this.
package com.test;
public class Test implements Testable {
public static void main(String[] args) {
Testable testable = new Test();
// OR
Test test = new Test();
if (testeable instanceof Testable)
System.out.println("instanceof succeeded");
if (test instanceof Testable)
System.out.println("instanceof succeeded");
}
}
interface Testable {
}
ie, any runtime instance that implements a particular interface will pass the instanceof
test
EDIT
and the output
instanceof succeeded
instanceof succeeded
@RohitJain
You can create instances of interfaces by using anonymous inner classes like this
Runnable runnable = new Runnable() {
public void run() {
System.out.println("inside run");
}
};
and you test the instance is of type interface, using instanceof
operator like this
System.out.println(runnable instanceof Runnable);
and the result is 'true'
When is using instanceof the right decision?
Legacy code or APIs outside of your control are a legitimate use-case for instanceof
. (Even then I'd rather write an OO layer over it, but timing sometimes precludes a redesign like that.)
In particular, factories based on external class hierarchies seem a common usage.
When should and shouldn't instanceof be used?
I find a need to use instanceof hints at bad design. It's a sure sign that a big, complex switch-style construct will follow. Most other times I see it used, we should use polymorphism rather than instanceof. See the Strategy pattern. (relevant examples of use)
The only time I find I need to use it is when implementing equals(Object o)
.
Is it possible to use the instanceof operator in a switch statement?
This is a typical scenario where subtype polymorphism helps. Do the following
interface I {
void do();
}
class A implements I { void do() { doA() } ... }
class B implements I { void do() { doB() } ... }
class C implements I { void do() { doC() } ... }
Then you can simply call do()
on this
.
If you are not free to change A
, B
, and C
, you could apply the visitor pattern to achieve the same.
Related Topics
Difference Between Regex [A-Z] and [A-Za-Z]
Simple Http Server in Java Using Only Java Se API
Antlr: Is There a Simple Example
Converting Array to List in Java
What Is Web-Inf Used for in a Java Ee Web Application
Add Image Thumbnails to a Layout in a Grid
Random Weighted Selection in Java
Output in a Table Format in Java's System.Out
Way to Get Number of Digits in an Int
Swing Grouplayout: Resizing and Limiting Component Sizes
Why Should Java 8's Optional Not Be Used in Arguments
How to Create a Rest Client for Java
Is There a Destructor for Java
Look and Feel Is Not Updating in Swing Jtabbedpane