Hi to all,
Hope you all will be fine. Actually i read a Java certification book in which i encountered these statements
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)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);
Can someone explain me this why 11 is small for byte?
Thanks




Reply With Quote