Results 1 to 4 of 4
- 10-14-2012, 09:43 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 5
- Rep Power
- 0
difference between >> Double.valueOf("12").doubleValue() and Double.valueOf("12") ?
i use this two in converting Strings to numbers...
for what i understood i think its they same but i know that there not they same in java meaning..
i want to know there difference and compare two of them
output is they same because there identical,Java Code:public class Main { public static void main(String[] args) { String s = "12.55"; double d1 = Double.valueOf(s); double d2 = Double.valueOf(s).doubleValue(); System.out.println(d1); System.out.println(d2); } }
i want to know the exact definition between this two string class method..
plz explain to me the difference or give me advantage if using one against other..
- 10-14-2012, 10:15 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: difference between >> Double.valueOf("12").doubleValue() and Double.valueOf("12")
There is no difference. Since java 5 there is the autoboxing mechanism! The compiler automatically add the xxxValue() methods if you need a primitive on the left side and have a wrapper object on the right.
So line 5 and 6 are identical (both are Double.valueOf(s).doubleValue()) after compiling!
- 10-14-2012, 10:20 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: difference between >> Double.valueOf("12").doubleValue() and Double.valueOf("12")
The "New to Java" answer is that they're the same. Use whichever you find clearer. I would go for the first version as the second would make me stop 6 months later and ask "Why doubleValue()? Is there something tricky going on?" and I would have to spend time verifying that there was nothing tricky that could be going on.
The other answer is more verbose and involves looking up, reading and thinking about the documentation, but it arrives at the same place.
Double.valueOf(s) returns a reference to a Double which is then converted into a double value that is assigned to d. This sort of conversion is called "unboxing".double d1 = Double.valueOf(s);
The place to run down exactly what is going to happen is the Java Language Specification. There is an assignment going on, so we look at 5.2. Assignment Conversion. "Assignment conversion occurs when the value of an expression is assigned (§15.26) to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of one of the following: ... an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion. ..."
We needn't worry about the widening primitive conversion: it's the sort of thing that might happen if a Float were being converted to a double. It would go Float-float (unboxing) followed by float->double (widening).
The interesting bit is 5.1.8. Unboxing Conversion and there we read "Unboxing conversion converts expressions of reference type to corresponding expressions of primitive type. Specifically, the following eight conversions are called the unboxing conversions: ... From type Double to type double. At run-time, unboxing conversion proceeds as follows: ... If r is a reference of type Double, then unboxing conversion converts r into r.doubleValue()".
And that's it really. "double d = Double.valueOf(s)" assigns to d the double value that would be returned by doubleValue().
(For completeness you should consult the links above and determine what would happen if the String s were null.)
- 10-14-2012, 11:10 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
"double" data type problem
By jackripley in forum New To JavaReplies: 2Last Post: 11-25-2011, 09:00 AM -
What is the difference between an "Entity" and "Value Object"?
By vahini in forum Advanced JavaReplies: 4Last Post: 06-14-2011, 04:59 AM -
Selecting the greatest "double" in a string array
By gangsterooseven in forum New To JavaReplies: 6Last Post: 11-07-2009, 11:37 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
represent Double as "" instead of 0.0 in .jsp page without javascript
By Tokajac in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 08-07-2008, 02:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks