Questions tagged [instanceof]

instanceof is an operator available in some object-oriented languages, including Java, php and JavaScript. Generally speaking, it allows the programmer to check whether an object passed as its left operand is an instance of a class specified by the right operand.

instanceof is an operator available in some object-oriented languages, including Java, php and JavaScript. It allows the programmer to check whether an object passed as its left operand is an instance of a class specified by the right operand.

instanceof in Java

instanceof in php

instanceof in JavaScript

958 questions
169
votes
9 answers

Java: Instanceof and Generics

Before I look through my generic data structure for a value's index, I'd like to see if it is even an instance of the type this has been parametrized to. But Eclipse complains when I do this: @Override public int indexOf(Object arg0) { if…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
160
votes
3 answers

How to test if one java class extends another at runtime?

How to I test if a is a subclass of b? Class a = A.class; Class b = B.class;
Armand
  • 23,463
  • 20
  • 90
  • 119
141
votes
4 answers

instanceof Vs getClass( )

I see gain in performance when using getClass() and == operator over instanceOf operator. Object str = new Integer("2000"); long starttime = System.nanoTime(); if(str instanceof String) { System.out.println("its string"); } else { if (str…
Dhananjay
  • 3,903
  • 2
  • 29
  • 44
117
votes
5 answers

Check instanceof in stream

I have the following expression: scheduleIntervalContainers.stream() .filter(sic -> ((ScheduleIntervalContainer) sic).getStartTime() != ((ScheduleIntervalContainer)sic).getEndTime()) .collect(Collectors.toList()); ...where…
quma
  • 5,233
  • 26
  • 80
  • 146
117
votes
6 answers

How to see if an object is an array without using reflection?

How can I see in Java if an Object is an array without using reflection? And how can I iterate through all items without using reflection? I use Google GWT so I am not allowed to use reflection :( I would love to implement the following methods…
edbras
  • 4,145
  • 9
  • 41
  • 78
113
votes
10 answers

Avoiding instanceof in Java

Having a chain of "instanceof" operations is considered a "code smell". The standard answer is "use polymorphism". How would I do it in this case? There are a number of subclasses of a base class; none of them are under my control. An analogous…
104
votes
4 answers

Why can't a "Class" variable be passed to instanceof?

Why doesn't this code compile? public boolean isOf(Class clazz, Object obj){ if(obj instanceof clazz){ return true; }else{ return false; } } Why I can't pass a class variable to instanceof?
eric2323223
  • 3,518
  • 8
  • 40
  • 55
101
votes
6 answers

Test if object is instanceof a parameter type

Is there a way to determine if an object is an instance of a generic type? public test(Object obj) { if (obj instanceof T) { ... } } That clearly doesn't work. Is there an alternative? Like I want to use Java reflection to…
Nikordaris
  • 2,297
  • 5
  • 20
  • 30
100
votes
8 answers

Is there something like instanceOf(Class c) in Java?

I want to check if an object o is an instance of the class C or of a subclass of C. For instance, if x is of class Point I want x.instanceOf(Point.class) to be true and also x.instanceOf(Object.class) to be true. I want it to work also for boxed…
snakile
  • 52,936
  • 62
  • 169
  • 241
95
votes
5 answers

What is the C# equivalent to Java's isInstance()?

I know of is and as for instanceof, but what about the reflective isInstance() method?
diegogs
  • 2,036
  • 2
  • 16
  • 20
90
votes
2 answers

The 'instanceof' operator behaves differently for interfaces and classes

I would like to know regarding following behavior of instanceof operator in Java. interface C {} class B {} public class A { public static void main(String args[]) { B obj = new B(); System.out.println(obj instanceof A); …
Ajay Sharma
  • 846
  • 10
  • 21
81
votes
7 answers

How to check if a subclass is an instance of a class at runtime?

In an android app test suite I have a class like this where B is a view: public class A extends B { ... etc... } now I have a list of view objects which may contain A objects but in this case I only care if they're subclasses or "instances of" B.…
iamamused
  • 2,720
  • 3
  • 25
  • 17
79
votes
2 answers

Checking if a class is java.lang.Enum

I'm trying to know if a class is an Enum, but I think I'm missing something: if (test.MyEnum.class instanceof Enum.class) obj = resultWrapper.getEnum(i, test.MyEnum.class); else obj = resultWrapper.getObject(i); It gives me an error saying…
Jose L Martinez-Avial
  • 2,191
  • 4
  • 28
  • 42
74
votes
7 answers

Is instanceof considered bad practice? If so, under what circumstances is instanceof still preferable?

Over the years, I've tried to avoid instanceof whenever possible. Using polymorphism or the visitor pattern where applicable. I suppose it simply eases maintenance in some situations... Are there any other drawbacks that one should be aware of? I do…
aioobe
  • 413,195
  • 112
  • 811
  • 826
73
votes
4 answers

Get type of a variable in Kotlin

How can I find the variable type in Kotlin? In Java there is instanceof, but in Kotlin it does not exist: val properties = System.getProperties() // Which type?
matteo
  • 2,121
  • 4
  • 20
  • 33
1
2
3
63 64