Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Possible Loss of Precision

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Possible Loss of Precision

    I have this code.
    char[] clearText = clearString.toCharArray();	
    char[] key = keyString.toCharArray();
    char[] cypherText = new char[clearText.length];
    for (int x = 0; x < clearText.length; x++) {
    	cypherText[x] = key[x] + clearText[x];
    	System.out.println(cypherText[x]);
    }
    It is giving the error a Possible Loss of Precision at this line, for apparently no reason.
    char letter = key[x] + clearText[x];
    Please note that the strings that are not shows are just strings, and are the same length.
    EDIT: Made the code less confusing
    Last edited by Canadian_Pirate; April 9th, 2011 at 08:57 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Possible Loss of Precision

    Quote Originally Posted by Canadian_Pirate View Post
    for apparently no reason.
    Of course there is a reason. do you really think that the creators of Java are in the habit of making up stuff gfro no reason?

    The result of your addition is a 32 bit int. You are trying to stuff it into a 16 bit char. Hence the loss of precision.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Possible Loss of Precision

    Quote Originally Posted by Junky View Post
    Of course there is a reason. do you really think that the creators of Java are in the habit of making up stuff for no reason?
    Sometimes the reason's quite poor

    @OP:

    When you try to add characters, Java will first implicitly cast them to an int. The reason for this is because the creators of Java are trying to get people away from the mentality that the char data type can be used as a number data type. However, since there's a lot of times that using the data from a char as an integer data type is very useful, you couldn't completely segregate the two types. So they just had math operations on char data types implicitly cast up to int's (bytes are too small, shorts technically work except for the issue of being signed where-as char's are technically unsigned, though there's not much of a difference other than interpretation).

    Once you have an integer result, you can't implicitly cast back to a char (due to the possible loss of precision), you must explicitly cast it back.

Similar Threads

  1. Possible loss of precision (double/int)
    By haloboy in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 8th, 2011, 02:23 AM
  2. set Precision
    By sabir in forum Java Theory & Questions
    Replies: 1
    Last Post: March 4th, 2011, 11:32 AM
  3. My code is having error, may be a lost of precision
    By siaosiaoboii in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 31st, 2011, 07:42 AM
  4. [SOLVED] Loss of Precision (Double/Int)
    By Scotty in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 9th, 2010, 01:45 PM
  5. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM