Results 1 to 3 of 3
  1. #1
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    56
    Rep Power
    0

    Default will valueOf of String ever lose data with double?

    I have a double primitive and gave it a huge value. I am curious if valueOf could ever potentially cause data loss. I tried a simple test and it seems it never loses any data:
    Java Code:
    public class DoubleStringTest {
    
    	public static void main(String[] args) {
    		double val = 1029.129348558634;
    		System.out.println(val);
    		System.out.println(String.valueOf(val));
    
    	}
    
    }
    Does this always hold true?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: will valueOf of String ever lose data with double?

    I think the byte code generated by the compiler for line 5 is the same as what you've coded on line 6.

    This might have some useful info: http://docs.oracle.com/cd/E19957-01/..._goldberg.html
    Last edited by Norm; 07-06-2014 at 12:17 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,913
    Rep Power
    5

    Default Re: will valueOf of String ever lose data with double?

    Quote Originally Posted by johnmerlino View Post
    I have a double primitive and gave it a huge value. I am curious if valueOf could ever potentially cause data loss. I tried a simple test and it seems it never loses any data:
    Java Code:
    public class DoubleStringTest {
    
        public static void main(String[] args) {
            double val = 1029.129348558634;
            System.out.println(val);
            System.out.println(String.valueOf(val));
    
        }
    
    }
    Does this always hold true?
    Why would it not hold true? The statement:

    double val = 1029.129348558634;

    Starts out as a string the compiler processes. And when you print it back out (with or without valueOf()), it is returned to string format. The only requirement is that it must be representable in IEEE 754 format when stored
    in memory as a primitive.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. toString() vs valueOf()
    By dojob in forum New To Java
    Replies: 9
    Last Post: 09-22-2013, 04:16 AM
  2. Replies: 3
    Last Post: 10-14-2012, 12:10 PM
  3. Replies: 3
    Last Post: 02-10-2012, 07:26 PM
  4. String to double HELP please!
    By zhen1337 in forum New To Java
    Replies: 33
    Last Post: 02-08-2011, 10:30 AM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM

Posting Permissions

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