Results 1 to 7 of 7
  1. #1
    j2vdk's Avatar
    j2vdk is offline Member
    Join Date
    Aug 2008
    Posts
    45
    Rep Power
    0

    Default help me- loss of precision error??

    i want to add 2 byte variables

    getting loss of precision error,,,,,

    how to add them,,,,


    class bytedemo{
    public static void main(String ar[]){
    byte a=10;
    byte b=5;
    byte c=a+b;
    System.out.println(c);
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Change this as follows.

    Java Code:
    byte c= (byte) (a + b);
    All variable lower that int are assign to int, unless you don't have specify.

    Check the following code segment.

    Java Code:
    byte a=10;
    byte b=5;
    int c=a+b;
    System.out.println(c);

  3. #3
    j2vdk's Avatar
    j2vdk is offline Member
    Join Date
    Aug 2008
    Posts
    45
    Rep Power
    0

    Default

    thank you sir

  4. #4
    j2vdk's Avatar
    j2vdk is offline Member
    Join Date
    Aug 2008
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Change this as follows.

    Java Code:
    byte c= (byte) (a + b);
    All variable lower that int are assign to int, unless you don't have specify.

    Check the following code segment.

    Java Code:
    byte a=10;
    byte b=5;
    int c=a+b;
    System.out.println(c);
    can u tell me the order, which u say lower , higher,,,,

    from wer to wer conversions happen automatically,,, and in which cases we have to specify explicitly

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by j2vdk View Post
    from wer to wer conversions happen automatically,,, and in which cases we have to specify explicitly
    Conversion is happen automatically, if you are not specifically define.

    byte and short variables are automatically convert into int, if you are not specifically define. See the example below.

    Java Code:
        short a = 12;
        short b = 23;
        short c = a + b;
    In third line you are looking a short value, as c. But actually what you have found on right side is int value. Once you added two short value, compiler assign the value to an int, looking to add in 32 bits.

    Same thing is happen for byte variables as well, I'll explain in the previous code.

    On the other hand, float values also assign to double.

    If you want to stuck with the same type, need explicit cast.

  6. #6
    j2vdk's Avatar
    j2vdk is offline Member
    Join Date
    Aug 2008
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post


    Conversion is happen automatically, if you are not specifically define.

    byte and short variables are automatically convert into int, if you are not specifically define. See the example below.

    Java Code:
        short a = 12;
        short b = 23;
        short c = a + b;
    In third line you are looking a short value, as c. But actually what you have found on right side is int value. Once you added two short value, compiler assign the value to an int, looking to add in 32 bits.

    Same thing is happen for byte variables as well, I'll explain in the previous code.

    On the other hand, float values also assign to double.

    If you want to stuck with the same type, need explicit cast.
    thank u ,,, its clear now

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Connection Loss
    By CrazyShells Slam in forum New To Java
    Replies: 0
    Last Post: 05-15-2008, 03:56 PM
  2. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 05:28 PM
  3. Replies: 7
    Last Post: 01-12-2008, 10:02 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
  •