CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2010
    Posts
    73

    Question question reagrding byte and integer size holder

    Hi to all,
    Hope you all will be fine. Actually i read a Java certification book in which i encountered these statements

    Code:
    byte a = 3; // No problem, 3 fits in a byte
    byte b = 8; // No problem, 8 fits in a byte
    byte c = b + c; // Should be no problem, sum of the two bytes fits in a byte
    
    The last line won't compile! You'll get an error something like this:
    
    TestBytes.java:5: possible loss of precision
    found : int
    required: byte
    byte c = a + b;
    ^
    We tried to assign the sum of two bytes to a byte variable, the result of which
    (11) was definitely small enough to fit into a byte, but the compiler didn't care. It
    knew the rule about int-or-smaller expressions always resulting in an int. It would
    have compiled if we'd done the explicit cast:
    byte c = (byte) (a + b);
    I want to ask how 11 is small enough to fit in a byte? 11 in binary is 1011. Since byte is 8 bit so this will become 00001011. Byte minimum range is (-2^7) and maximum range is (2^7 - 1)

    Can someone explain me this why 11 is small for byte?

    Thanks

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: question reagrding byte and integer size holder

    This is because the '+' operator is not overloaded to handle bytes. Instead it converts them to ints and performs the calculation. The possible loss of precision is because you now have an int representing 11 that you are placing into a byte.

    When you mix integer types such as short, long, int and byte, the 'wider' type (larger bit wise) is returned.

    So an int plus a long will return a long, for example.
    Last edited by ProgramThis; September 7th, 2011 at 08:40 AM.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: question reagrding byte and integer size holder

    Quote Originally Posted by Basit781 View Post
    Can someone explain me this why 11 is small for byte?
    This will compile,

    byte b = 11;

    but this will give a warning,

    byte b = (int)11;

    It's the same integral literal but in the first case the compiler considers 11 to be a byte whereas in the second case you instruct the compiler by explicit casting to consider 11 an int.

    So the compiler acts on type information and not on the literal itself. The compiler infers the type from context or from implicit or explict casting according to Java language rules.

    This careful review of type integrity is a very important compiler job. Or expressed differently - Java is a strongly typed language.
    Last edited by nuzzle; September 7th, 2011 at 12:32 PM.

  4. #4
    Join Date
    Jul 2010
    Posts
    73

    Re: question reagrding byte and integer size holder


    because you now have an int representing 11 that you are placing into a byte.

    So the compiler acts on type information and not on the literal itself. The compiler infers the type from context or from implicit or explict casting according to Java language rules.
    Hmm thanks to you both. Now i understood.

    Thanks

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured