Results 1 to 11 of 11
  1. #1
    greenbean is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

    Question Double.valueOf() vs Double.parseDouble()

    Is there any difference/advantage between using the the Double.valueOf() method vs Double.parseDouble() method?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Depends on whether you want a double or a Double. Although with autoboxing, the end effect is the same, it is a few nano seconds slower (not that that really makes a difference, except maybe in simulation programs where an ungodly amount of calculations/conversion might be taking place, and then only as one of the last considerations).

    I would still, however, advocate using the proper one according to the desired result.

  3. #3
    greenbean is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

    Default

    Thanks masijade, so if the program is doing some intensive mathes compuitation, its better to use the Double.parseDouble() method?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    It is better to use the one that is needed. It depends on what you are doing. I said, that if it is very intensive then you want to avoid using doubles in places where Doubles are needed in order to avoid the autoboxing overhead. But, it would need to be very, very, very, intensive before it actually makes any difference.

    So, which one you use depends on what you're doing, and what the rest of the program will expect.

  5. #5
    greenbean is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

    Default

    Thanks again, I agree the speed difference is minimal. I get a bit about autoboxing though; does autoboxing convert primitive type to class object, or the other way round? If autoboxing is the conversion of primitive type to wrapper object, then it seems Double.valueOf() method would be the prefered one to use then.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    It "converts" both ways. So, once again, use the one that returns the type that you need.

  7. #7
    greenbean is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

    Default

    "both ways"? I thought it's one way, as there is another process called autounboxing.
    Say if I need to manipulate the operand in an ArrayList, so the operand would need to be of a wrapper type (i.e. Double), so I would define the operand as Double and use the Double.valueOf() method, then there wouldn't be any autoboxing, right?
    Then when I do mathes computation, the operand should not need to be convert to primitive type of double to carry out the operation, right?
    So I think it would be saver to define the operand as Double and use Double.valueOf() method.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Yes, it is, technically autoboxing - autounboxing, but it is all the same process. An automatic "conversion" from primitive to Object to primitive.

    If you are storing the results of the method in an ArrayList (before doing any calculations with it), then yes, use the one that returns a Double. If you are going to be doing calculations with the return value before placing it in the ArrayList, I, personally, would use the one that returns a double.

  9. #9
    greenbean is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

    Default

    So, do you think doing calculation on wrapper type require autoboxing?

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Technically, it would require autounboxing, but yes. I don't think so, I know so, unless you do the math on Double.doubleValue(), rather than on the Double itself. And then, if you are assigning the result to a Double, it would also need autoboxing (in addition to the unboxing in the calculation itself).

    Edit: This is all rather academic though. What is it you are actually having problems with. If you are seeing a performance problem, this is one of the last things to look toward. And if you are looking to prevent future performance problems, this is one of the last things to look toward. Any performance advantage is extremely small. Usually worth much less than the effort to change the code.
    Last edited by masijade; 01-12-2009 at 09:40 AM.

  11. #11
    greenbean is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Using double for money is a sin
    By fishtoprecords in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 08:05 AM
  2. Double Buffering
    By dunff2k in forum SWT / JFace
    Replies: 1
    Last Post: 11-14-2008, 04:26 PM
  3. Double Value problem
    By sakthivel123 in forum New To Java
    Replies: 2
    Last Post: 07-10-2008, 05:18 PM
  4. Calculating sin of a double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 09:13 PM
  5. Getting smallest possible Double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •