This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

instanceof key word

 
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
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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...
 
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
 
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.
 
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
 
author
Posts: 23843
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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 ]
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!