File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Map Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Map Question" Watch "Map Question" New topic
Author

Map Question

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,



Source of the question
Please guide me!

Thanks and Regards,
cmbhatt
[ April 04, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
LinkedHashMap is a subclass of HashMap.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Keith,

One more doubt:



It may be silly to ask, but my brain is not responding?

I preferred not to start a new thread and continue with this. Is that OK?

Source of the question

Thanks and Regards,
cmbhatt
[ April 04, 2007: Message edited by: Chandra Bhatt ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42274
    
  64

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 ]

Ping & DNS - my free Android networking tools app
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Ulf,


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
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, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Chandra Bhatt
Ranch Hand

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

ArrayList al = new ArrayList();
No IS-A between ArrayList and Collections so the compiler error.

Correct me if I still miss something!

Thanks and Regards,
cmbhatt
gianni ipez
Ranch Hand

Joined: Jan 02, 2007
Posts: 65
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: 42274
    
  64
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: 2367
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.


The form of an instanceof expression is

RelationalExpression instanceof ReferenceType


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.

Type Comparison Operator instanceof
sravanthi pulukuri
Ranch Hand

Joined: Mar 15, 2007
Posts: 125
Can you explain what you exactly mean that sentence
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42274
    
  64
Originally posted by sravanthi pulukuri:
Can you explain what you exactly mean that sentence


Which sentence?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Map Question