I learned that Java has the instanceof operator. Can you elaborate where it is used and what are its advantages?
- 4Have you had a look at this?user545680– user5456802011-09-23 09:24:56 +00:00Commented Sep 23, 2011 at 9:24
- 2This SO link should give you a lot of idea: stackoverflow.com/questions/496928/…Scorpion– Scorpion2011-09-23 09:28:54 +00:00Commented Sep 23, 2011 at 9:28
- 4If I google your question I get 11.7 million results. Is there something you would like to know which has not already been discussed at length?Peter Lawrey– Peter Lawrey2011-09-23 09:51:21 +00:00Commented Sep 23, 2011 at 9:51
- 34Dup maybe, but questions like this make SO a great resource across all skill levels. I am glad this was the top result when I goog'd.Edward Newell– Edward Newell2014-02-07 01:57:42 +00:00Commented Feb 7, 2014 at 1:57
- Here's a good article on the use of this: javatpoint.com/downcasting-with-instanceof-operatorJason– Jason2017-08-10 13:25:10 +00:00Commented Aug 10, 2017 at 13:25
4 Answers
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).
4 Comments
Integer.class format actually legal? When I attempt to use it in your example, in Eclipse, I get Syntax error on token "class", Identifier expected. However, switching it to simply Integer works fine..equals() methods. it's common for intelliJ to generate equals methods that use instanceofinstanceof is used to check if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
Comments
instanceof can be used to determine the actual type of an object:
class A { } class C extends A { } class D extends A { } public static void testInstance(){ A c = new C(); A d = new D(); Assert.assertTrue(c instanceof A && d instanceof A); Assert.assertTrue(c instanceof C && d instanceof D); Assert.assertFalse(c instanceof D); Assert.assertFalse(d instanceof C); } 4 Comments
instanceof to determine the type of tokens (e.g. Identifier, Literal, etc..., extending from Token). If I was not going to use instanceof, then I'd have an unique Token class and would have to create various unnecessary different type of fields to hold the value of the actual token.instanceof is a keyword that can be used to test if an object is of a specified type.
Example :
public class MainClass { public static void main(String[] a) { String s = "Hello"; int i = 0; String g; if (s instanceof java.lang.String) { // This is going to be printed System.out.println("s is a String"); } if (i instanceof Integer) { // This is going to be printed as autoboxing will happen (int -> Integer) System.out.println("i is an Integer"); } if (g instanceof java.lang.String) { // This case is not going to happen because g is not initialized and // therefore is null and instanceof returns false for null. System.out.println("g is a String"); } } Here is my source.
3 Comments
if? Now the second and third conditions aren't evaluated since the first is true.