• Post Reply 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Map Question

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LinkedHashMap is a subclass of HashMap.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain what you exactly mean that sentence
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sravanthi pulukuri:
Can you explain what you exactly mean that sentence



Which sentence?
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic