Results 1 to 11 of 11
- 01-11-2009, 10:29 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 7
- Rep Power
- 0
- 01-11-2009, 10:53 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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.
- 01-11-2009, 11:03 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 7
- Rep Power
- 0
Thanks masijade, so if the program is doing some intensive mathes compuitation, its better to use the Double.parseDouble() method?
- 01-12-2009, 12:00 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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.
- 01-12-2009, 07:41 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 7
- Rep Power
- 0
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.
- 01-12-2009, 07:44 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
It "converts" both ways. So, once again, use the one that returns the type that you need.
- 01-12-2009, 07:57 AM #7
Member
- Join Date
- Dec 2008
- Posts
- 7
- Rep Power
- 0
"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.
- 01-12-2009, 08:16 AM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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.
- 01-12-2009, 08:28 AM #9
Member
- Join Date
- Dec 2008
- Posts
- 7
- Rep Power
- 0
So, do you think doing calculation on wrapper type require autoboxing?
- 01-12-2009, 08:37 AM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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 08:40 AM.
- 01-12-2009, 08:39 AM #11
Member
- Join Date
- Dec 2008
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Using double for money is a sin
By fishtoprecords in forum New To JavaReplies: 3Last Post: 11-28-2008, 07:05 AM -
Double Buffering
By dunff2k in forum SWT / JFaceReplies: 1Last Post: 11-14-2008, 03:26 PM -
Double Value problem
By sakthivel123 in forum New To JavaReplies: 2Last Post: 07-10-2008, 04:18 PM -
Calculating sin of a double value
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 08:13 PM -
Getting smallest possible Double value
By Java Tip in forum Java TipReplies: 0Last Post: 12-06-2007, 02:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks