Thanks and Regards, cmbhatt [ April 04, 2007: Message edited by: Chandra Bhatt ]
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35258
7
posted
0
Object a = new ArrayList(); System.out.print((a instanceof Collections)+","); //No error, why?
It doesn't matter that the type of the reference if Object - a is an ArrayList, and thus an instance of Collection. Not of Collections, though.
Animal a1= new Animal(); System.out.println("\n" + (a1 instanceof Collections)); //why error here
Yes, Animal extends Object, so "a1 instanceof Object" would be true, but it does not extend Collection. [ April 05, 2007: Message edited by: Ulf Dittmer ]
Object a = new ArrayList(); System.out.print((a instanceof Collections)+","); //No error, why?
It is Collections, Collections is a class; runtime type of the a would be ArrayList. What is association between Collections and ArrayList? Please clarify.
Thanks and Regards, cmbhatt
Srinivasan thoyyeti
Ranch Hand
Joined: Feb 15, 2007
Posts: 557
posted
0
Hi Chandra,
case 1> Object a = new ArrayList(); System.out.print((a instanceof Collections)+",");
Compiler : By seeing "a instanceof Collections" thinks, a which is object reference can be convertible to any compatible type. So it will not complain rather it don't know what the object pointing to.
Case 2> Compiler can only retrict or throw error when it knows type of the LHS operand. Like ... ArrayList a = new ArrayList(); System.out.print((a instanceof Collections)+",");
Here copiler throws error bec'ze there is no IS-A correspondence between them.
Thats the situation with compiler (Type place a role here).
JVM(runtime): returns true or false for the statement.
Hope its clear.
Thanks & Regards,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
posted
0
Thanks Srini,
Oh Yeah, I was missing the vital part that was IS-A. Thanks for helping me to recall. what I got: class Collections extends Object (ofcourse as everyclass do). So compiler can only see the reference variable that is of Object class and LHS Collections (It is ok because, Collections extends Object). At run time returns false, becaue a's run timetype is ArrayList.
interesting to know: instanceof operator can just be used to relate objects at runtime. at compile time you already know if the 2 objects can be ever related, so the compiler prevents you to use if there is no need.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35258
7
posted
0
What is association between Collections and ArrayList? Please clarify.
There's no association between ArrayList and Collections whatsoever. There is an association between ArrayList and Collection in the sense that an ArrayList is an instance of Collection. [ April 05, 2007: Message edited by: Ulf Dittmer ]
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
Originally posted by gianni ipez: interesting to know: instanceof operator can just be used to relate objects at runtime. at compile time you already know if the 2 objects can be ever related, so the compiler prevents you to use if there is no need.
If the type of the RelationalExpression could not be cast to the ReferenceType without causing a ClassCastException, then the instanceof operator will cause a compile-time error.