Results 1 to 4 of 4
  1. #1
    iamchicken is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default 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

    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);
        }
    }
    output is they same because there identical,
    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..

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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!

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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 d1 = Double.valueOf(s);
    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".

    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.)

  4. #4
    iamchicken is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: difference between >> Double.valueOf("12").doubleValue() and Double.valueOf("12")

    thank you guyz for helpin me

Similar Threads

  1. "double" data type problem
    By jackripley in forum New To Java
    Replies: 2
    Last Post: 11-25-2011, 10:00 AM
  2. Replies: 4
    Last Post: 06-14-2011, 05:59 AM
  3. Selecting the greatest "double" in a string array
    By gangsterooseven in forum New To Java
    Replies: 6
    Last Post: 11-08-2009, 12:37 AM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. represent Double as "" instead of 0.0 in .jsp page without javascript
    By Tokajac in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-07-2008, 03:49 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
  •