Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof key word

 
Asanka Vithanage
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
class Col1{
public static void main(String []e){
Set aa=new HashSet();
aa.add(1);
aa.add(2);
aa.add(3);
aa.add(4);
aa.add(5);
//System.out.println(aa instanceof Integer);//1
//System.out.println(aa instanceof Byte);//2
System.out.println(aa instanceof Number);

}
}
this code compiles and gives "false" as out put
but if i uncomment 1 or 2 it gives compile error saying
inconvertible types
but we know Integer is a Number
can any body explain this
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are trying to compare a Set (aa is a Set not Integer) and a Number. This is allowed as Set is an interface and Number is a non-final class. So in some cases, a Set might be a Number as there can be a class which implements Set and extends Number. But when you compare Set (i.e. aa) and Integer, it is not allowed as Integer is a final class. So in no way can aa be an instaceof Integer. Same is the case with Byte...
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Ankit are you saying that if it is a final class then you cannot use the instanceof operator? I mean String is a final class but I would assume that you can use the instanceof on a String object. Pardon me if I asked a very basic questio.
Thanks,
Meera
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what Ankit tried to mean is -

as Number is a non-final class, any class can implement Set and extend Number, in which case, all of these three fall in same inheritance hierarchy. So, instanceOf operator works fine.

but nowhere in the inheritance tree of Integer, Set is implemented and again, Integer can not be extended as it's a final class. So, it's not possible to create a class which implements Set and extends Integer, both. Therefore use of instanceOf operator with Set and Integer will produce compile time error.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed to Ankit!!

Meera,

Did you test (aSet instanceof String)?
It fails to compile because of the above given reasons.

It works for Number because you may have situation like:



In this case,
YourSet urset = new YourSet();
(urset instanceof Number) is fine
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I did, and I get a compiler error. So the same explanation holds true for String as well. So am I correct in saying that you cannot use "instanceof" test on final classses?
Thanks,
Meera
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by meera kanekal:
Yes I did, and I get a compiler error. So the same explanation holds true for String as well. So am I correct in saying that you cannot use "instanceof" test on final classses?
Thanks,
Meera


No. It is not correct. Of course, you can use the instanceof operator with final classes.

The issue here is with the ability to determine the result at compile time, and determined it as false. If, at compile time, the compiler can guarrantee that there is no way that the object is an instance of a particular class or interface, it will report it as a failure at compile time.

And how the compiler makes such a determination is based on the reference type being checked, the type it is checking, and/or whether these types are final or not.

Henry
[ December 07, 2008: Message edited by: Henry Wong ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic