0
// Demonstrate a Hashtable
import java.util.*;
class HTDemo {
    public static void main(String args[]) {
        Hashtable balance = new Hashtable();
        Enumeration names;
        String str;
        double bal;
        balance.put("John Doe", new Double(3434.34));
        balance.put("Tom Smith", new Double(123.22));
        balance.put("Jane Baker", new Double(1378.00));
        balance.put("Todd Hall", new Double(99.22));
        balance.put("Ralph Smith", new Double(-19.08));

        // Show all balances in hash table.
        names = balance.keys();
        while(names.hasMoreElements()) {
            str = (String) names.nextElement();
            System.out.println(str + ": " +
            balance.get(str));
        }
        System.out.println();

        // Deposit 1,000 into John Doe's account
        ***bal = ((Double)balance.get("John Doe")).doubleValue();***
        balance.put("John Doe", new Double(bal+1000));
        System.out.println("John Doe's new balance: " +
        balance.get("John Doe"));
    }
}
  1. In line bal = ((Double)balance.get("John Doe")).doubleValue(); What is the use of doubleValue? (i know it converts object to double value ) but program runs okay if i run without this.
  2. (correct me if i am wrong) balance.get get here a double object of value 3434.34 and (double ) in front of it does unboxing and converts it into double object in double value then how and why does doubleValue() treats this double 3434.34 as object?????
vyegorov
  • 21,787
  • 7
  • 59
  • 73

3 Answers3

2

I am not sure if I got your questions correctly. I will try to explain from the perspective of what I understood your questions to be.

  1. Yes, you are right, you do not need doubleValue(). It is just a way of explicitly telling Java to unbox. If you do not use doubleValue(), Java will unbox it automatically. So, in case if Java had no support for automatically [un]boxing primitive types to the corresponding Object types, then you will need doubleValue() as (Double)balance.get("John Doe") will return an Object while bal is a variable of primitive type.
  2. For the second part of your question, in Java double is a primitive data type and Double is an object type. The Java collections API and hence Hashtable only store objects. They do not store primitive types. Hence, balance.get() returns an object. You cast it into another object of type Double. The doubleValue() method is optional here due to the automatic boxing/unboxing provided by Java.

Hope this helps!

Ankit
  • 6,772
  • 11
  • 48
  • 84
0

Collections work Only with OBJECT types and not the primitive ones, One of the reasons why the wrapper classes were created, you cannot store 3.4 but u can store new Double(3.4), Yes as you said, you can use the value without its doubleValue but its a standard way of doing things. And for the Implicit calls and conversions, there is a risk of different version of jvms/jre behaving slightly different in such cases, in order to avoid them you would like to do it the standard way , so that it doesnt get impacted

Akash Yadav
  • 2,411
  • 20
  • 32
  • 2
    There's no risk of different JVMs behaving differently. The boxing and unboxing is done by the javac compiler, not the JIT compiler. See [What code does the compiler generate for autoboxing](http://stackoverflow.com/questions/408661/what-code-does-the-compiler-generate-for-autoboxing) . The method `doubleValue()` exists because auto boxing and unboxing is simply syntactic sugar - it's not the 'standard' way of doing the conversion, it's the *actual* way of doing it. – Greg Kopff May 09 '12 at 05:01
  • Thanks Greg for the clarification :) – Akash Yadav May 09 '12 at 05:37
0
  1. In line bal = ((Double)balance.get("John Doe")).doubleValue(); what is the use of doubleValue (know it converts object to double value ) but program runs okay if i run without this.

Correct.

  1. (correct me if I am wrong) balance.get() gets a double object of value 3434.34 and (double) in front of it does unboxing

No. There is no (double). There is (Double). That typecasts it from Object to Double.

and converts it into double object

Yes.

in double value

No. It is an object not a value. This phrase is meaningless.

then how and why does doubleValue() treats this double 3434.34 as object?

It doesn't. It calls Double.doubleValue() which returns a double.

This code doesn't use autoboxing at all. The doublevalue() calls in this code have been obsolete since Java 1.5 came out about 5 years ago. It also doesn't use Generics. I suspect you are looking at some very old code.

user207421
  • 305,947
  • 44
  • 307
  • 483