This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
    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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

instanceof Comparison

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.??
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
instanceof returns true when IS-A condition is satisfied..
ex::

[ July 25, 2007: Message edited by: Priyam Srivastava ]
 
robin singal
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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...
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
all are true ....isn't it....
fingers crossed..
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Pls bear in mind that if

Obj b = null;

then
b instanceof Obj

will return false.
 
Priyam Srivastava
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
just to be more precise on what Victor said::

null instanceof (any class)---->false
 
robin singal
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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....
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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...
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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?
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hey Nik.......Thanks a lot for your explanation

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




Preparing SCJP 1.5
 
Amit Mahajan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Manfred

The following must be true:

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

Thanks.

Regards,
Amit
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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 !!
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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???
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Mahalakshmi,

Please DontWakeTheZombies. You can start new topics instead of replying to older topics.
 
Mahalakshmi Chandru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Treimin,
Thanks for the info. Will create a new thread..
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi All,
every one effort is very nice
thanks every body today i learned new topic
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    Bookmark Topic Watch Topic
  • New Topic