Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

HashMap<Object, Double> returns what instead of null?

Thread view: 
aaronfude@gmail.com - 09 May 2006 03:30 GMT
Hi,

What is get(Object) supposed to return when called on an object of type
HashMap<Object, Double> as in:

double x = hashMap.get(key);

when the key is not available?

Thanks!

Aaron Fude
Rhino - 09 May 2006 04:46 GMT
> Hi,
>
[quoted text clipped - 4 lines]
>
> when the key is not available?

Without actually trying it, I'm guessing that you'd get a null if you did a
get() on a key that didn't exist. But why guess? If I were you, I'd just
write a few lines of code then try it in your debugger and see what actually
happens. An actual experiment is always better than an educated guess if the
experiment isn't too hard to conduct.

--
Rhino
Simon - 09 May 2006 09:19 GMT
>>What is get(Object) supposed to return when called on an object of type
>>HashMap<Object, Double> as in:
[quoted text clipped - 5 lines]
> Without actually trying it, I'm guessing that you'd get a null if you did a
> get() on a key that didn't exist.

The problem is that a double cannot be null. Think of the above line of code as

double x = hashMap.get(key).doubleValue();

This will throw a NPE as will the original code. The same happens for

Double d = null;
double x = d;

Cheers,
Simon
Thomas Hawtin - 09 May 2006 09:35 GMT
> The problem is that a double cannot be null. Think of the above line of code as
>
[quoted text clipped - 4 lines]
> Double d = null;
> double x = d;

The outrageous dangers of auto*un*boxing...

If you do want a "null" for double, don't use a very small number.
Double.NaN makes a suitable substitute. Note, to check for NaNs use
Double.isNan. IEEE 754 demands Double.NaN != Double.NaN...

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Chris Uppal - 09 May 2006 10:48 GMT
> What is get(Object) supposed to return when called on an object of type
> HashMap<Object, Double> as in:
>
> double x = hashMap.get(key);
>
> when the key is not available?

Exactly the same as if you tried to unbox null.  I.e. the same as if you'd
executed:

    Double d = null;
    double x = d;

or, the exact equivalent to the above two lines:

   Double d = null;
   double x = d.doubleValue();

Why should you expect that to do anything except throw a NullPointerException ?

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.