Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof    key word Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof    key word" Watch "instanceof    key word" New topic
Author

instanceof key word

Asanka Vithanage
Ranch Hand

Joined: Oct 24, 2008
Posts: 59
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


SCJP 1.5 (94%)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
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

Joined: Oct 17, 2006
Posts: 140
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.


When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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


cmbhatt
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
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
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof key word
 
Similar Threads
using TreeSet TreeMap with Strings And Integers Problem
InstanceOf Doubt.
instanceof prblem help me
K&B book page 286 table 4-1: Is the array instnaceof result correct?
instanceof operator