Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map Question

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



Source of the question
Please guide me!

Thanks and Regards,
cmbhatt
[ April 04, 2007: Message edited by: Chandra Bhatt ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LinkedHashMap is a subclass of HashMap.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain what you exactly mean that sentence
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sravanthi pulukuri:
Can you explain what you exactly mean that sentence


Which sentence?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic