Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof Comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

instanceof Comparison

robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
Even if the object being tested is not an actual instantiation of the class type on the right side of the operator , "instanceof" will still return true if the object being compared is assignment compatible with the type on the right.
What does assignment compatible here means.??


Life is short play hard.
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
instanceof returns true when IS-A condition is satisfied..
ex::

[ July 25, 2007: Message edited by: Priyam Srivastava ]

"History would be kind to me, for I intend to write it."
robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
actually i got confused because of the quite contradictory results of the below two code lines:


interface Face {}
class Bar implements Face {}
class Foo extends Bar {}

First Operand instanceof Operand Result
(Type We're comparing
the reference against)

Bar instance Foo ??(what would be
result and why)



class A {}
class B extends A {
public static void main(String [] args) {
A myA = new B();
m2(myA);
}
public static void m2(A a) {
if (a instanceof B)
((B)a).doBstuff();
}
public static void doBstuff() {
System.out.println("'a' refers to a B");
}
}


what would be output of above program???

the first one is false and the second one do prints the output.
i'm confused by thought that if in first case bar extends foo is false (as it is foo which extends bar) then why in the second case a or A instanceof B
is true as it is B which extends A and not A which extends B.
Please remove my confusion>>>
robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
I tried making table so did't displayed right...
the first code is::

first operand -- Bar Instace

instanceof Operand -- Foo

Result -- False
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169

Bar instance Foo ??--->not necessarily..

if you have a code like::
Bar b = new Foo();
then (b instanceof Foo) returns true..
this is because when you use instanceof test..donot compare the class
type of reference variable with the class on the right side of
instanceof operator but compare the object that referene variable
refers to at run time...

here though class type of b is Bar but at run time it is referring to an
object of Foo and Foo IS A Foo.

similarly



in this code reference variable 'a' at run time refers to the object of B. and B IS A B.

hope this helps !!
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
if you got what i was trying to say in my last post then try answering this::



f instanceof Bar ?
f instanceof Face ?
f1 instanceof Bar ?
f1 instanceof Face ?
f1 instanceof Foo ?
b1 instanceof Bar ?
b1 instanceof Face ?
b1 instanceof Foo ?

just look at the object the reference varaible refers to at rum time and
you would be able to answer them !!
robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
ok i got the point that compare the object that reference variable
refers to at run time....
but what about the 1st code....it's result is depicted as false in K&B...
what should i conclude...for that...
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy ranchers,

robin asked:
W
hat does assignment compatible here means.??
(...)
Bar instance Foo ??



I think the second question has been answered already by Priyam.

The first: it will not compile, if the object variable you put before the instanceof operator is not in the same inheritance tree as the class name coming after the instanceof
eg.

will not compile.


It will compile only for useful (more or less...) comparisons and a String can never be an Integer.


But you can ask for any instance, if it is an instance of any of its super classes (or its "own" class) and also if it implements a certain interface.

For example:


Something else, robin, please read your private emails by clicking "My Profile" right to Moosey.


Yours,
Bu.
robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
all are true ....isn't it....
fingers crossed..
victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
Pls bear in mind that if

Obj b = null;

then
b instanceof Obj

will return false.
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
just to be more precise on what Victor said::

null instanceof (any class)---->false
robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
ya as null ref. variable means it refers nowhere and hence instanceof will be false ....rightly said...
apart from that are my answers right...are they all true....
Amit Mahajan
Greenhorn

Joined: Jul 13, 2007
Posts: 15
Thanks guys for clearing the confusion..
To summarize

class A{}
class B extends A{}
class C{}
.
.
.
A aa = new A();
B bb = new B();
A ab = new B();
C cc = new C{};

aa instanceOf A; //true
aa instanceOf B; //false
bb instanceOf A; //false
bb instanceof B; //true
ab instanceOf B; //true
ab instanceOf A; //false
cc instanceOf A; //compilation error (not in the class hierarchy)
cc instanceOf B; //compilation error (not in the class hierarchy)

Correct me if i am wrong.
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Amit Mahajan:
bb instanceOf A; //false
ab instanceOf A; //false

Not correct, since B IS-A(n) A.
robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
hello amit
what i think..

bb instanceof A //should be true

Explanation:

Because bb refers to B at runtime acc. to B bb = new B();
and as B extends A => B IS-A A and hence bb instanceof A is true

ab instanceof A //true
similar explanation for above


Rest of are right...
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
Hi All,

Every one has provided the good explanation cheers for that.

I just wanna add one thing to this, do make Inheritance diagram and just check what is the realtion between the classes in refrence and the object class. Is their any is-a relation?
Now very Child is-a intanceOf Parent.
but vice versa is not true.
and if their is no relation than off course exception.
If null than false.

____
| |
| A |
-----
|
is-a |
|
____
| |
| B |
-----


____
| |
| C |
-----


Now just look at the diagram and find the result.


http://subodh-gupta.blogspot.com
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Please if anyone can help me on this shown below..doubt..it will be kinda of you..

in the above shown post,it is said that




my doubt is

if B bb=new B();

than bb is instanceOf A
as well as B.

Than why in the statement

A aa =new A();
aa is instanceOf A and not B?
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Dhwani,
Analyze it on is-a relationship. Look at the below one

if B bb=new B();

than bb is instanceOf A
as well as B

bb is instanceOf A because A is the super class and B is the sub-class.So B is A.


A aa =new A();
aa is instanceOf A and not B because A is not B.A is the super class here.Rememember IS-A relationship.

Regards,
Nik
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hey Nik.......Thanks a lot for your explanation

.....now i am clear with my doubt




Preparing SCJP 1.5
Amit Mahajan
Greenhorn

Joined: Jul 13, 2007
Posts: 15
Hi Manfred

The following must be true:

bb instanceOf A; //true
ab instanceOf A; //true

Thanks.

Regards,
Amit
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Amit Mahajan:
The following must be true:

bb instanceOf A; //true
ab instanceOf A; //true
I know. I have only quoted the incorrect lines.
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
looking at the discussions on this topic.. instanceof seems to be really a "thorn in the flesh"
for many aspiring SCJPians!!

i hope this discussin really helped everyone !!
Mahalakshmi Chandru
Ranch Hand

Joined: Feb 26, 2009
Posts: 48
hi all,
I have a doubt in the below code. Somebody please clarify:



Foo [ ].instanceOf(Foo, Bar, Face) -> false // correct
Foo [ ].instanceOf(Object) -> true // correct
Foo [ 1 ].instanceOf(Foo, Bar, Face, Object) -> true // how???
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Mahalakshmi,

Please DontWakeTheZombies. You can start new topics instead of replying to older topics.
Mahalakshmi Chandru
Ranch Hand

Joined: Feb 26, 2009
Posts: 48
Treimin,
Thanks for the info. Will create a new thread..
NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
Hi All,
every one effort is very nice
thanks every body today i learned new topic


I know Life is very Tough...But I AM TOUGHER
SCJP 1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof Comparison
 
Similar Threads
instanceof Operator
instanceof operator
Test Question - Explanation?
Unreachable code
instanceof and isInstance