• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

instanceof key word

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic