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

Discussion in 'Java' started by aaronfude@gmail.com, May 9, 2006.

  1. Guest

    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
     
    , May 9, 2006
    #1
    1. Advertising

  2. Rhino Guest

    <> wrote in message
    news:...
    > 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?
    >

    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
     
    Rhino, May 9, 2006
    #2
    1. Advertising

  3. Simon Guest

    Rhino wrote:
    >>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?
    >>

    >
    > 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
     
    Simon, May 9, 2006
    #3
  4. Simon wrote:
    >
    > 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;


    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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, May 9, 2006
    #4
  5. Chris Uppal Guest

    wrote:

    > 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
     
    Chris Uppal, May 9, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vince Darley
    Replies:
    4
    Views:
    4,553
    emilchacko
    Mar 2, 2010
  2. vizlab
    Replies:
    3
    Views:
    4,271
    Michael Bar-Sinai
    Oct 17, 2007
  3. Sydex
    Replies:
    12
    Views:
    6,649
    Victor Bazarov
    Feb 17, 2005
  4. Rakesh
    Replies:
    10
    Views:
    12,268
    Mike Schilling
    Apr 8, 2008
  5. Replies:
    2
    Views:
    104
    Thomas 'PointedEars' Lahn
    Feb 12, 2006
Loading...

Share This Page