2

What happens when I assign value larger than byte?

According to official oracle documentation, Byte is

  • Byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation

My code is

public class B
{

    public static void main(String args[])
    {
        byte b;
        b=(byte)129;
        System.out.println("b="+b);

    }
}   

Output:

b=-127

What happens when I assign value larger than byte. Java compiler will report an error. I will go if I cast this value to byte

byte b = (byte) 128;

I do not understand the output of this program?

Aman
  • 45
  • 1
  • 4
  • 4
    overflow happens – Lino May 05 '19 at 08:27
  • 2
    @CarlosHeuberger why is the program output is 127 with a minus sign. should not it be 127 only? – Aman May 05 '19 at 08:29
  • 1
    my previous comment was for the question before you changed it (*"Error incompatible types: possible lossy conversion from int to byte. "*) ! Documentation: [5.1.3. Narrowing Primitive Conversion](https://docs.oracle.com/javase/specs/jls/se12/html/jls-5.html#jls-5.1.3) – user85421 May 05 '19 at 08:29
  • 1
    @CarlosHeuberger why is there a minus sign in output. Thanks for documentation link – Aman May 05 '19 at 08:33
  • 3
    Dr. Lanning: That, Detective, is the "right question." - I, Robot! because the highest bit of byte is set: 129 = `1000 0001` in binary - read the documentation: "*this may cause the sign of the resulting value to differ from the sign of the input value*" – user85421 May 05 '19 at 08:40
  • @Aman How does Java handle integer underflows and overflows and how would you check for it? https://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo This post is about int but concept is same for byte. – Pie May 05 '19 at 09:05
  • 1
    Note: only get compiler error when not converting to `byte` that is `b = 129; ` (without cast) – user85421 May 05 '19 at 09:20

3 Answers3

5

For byte type, you only have 8 bits to store the value. You can have only 256 distinct values (2^8 = 256). Java represents negative values with '1' as the highest bit:

-128 (dec) => 10000000 (bit)
-127 (dec) => 10000001 (bit)
... 
-1   (dec) => 11111111 (bit)
0    (dec) => 00000000 (bit)
+1   (dec) => 00000001 (bit)
+127 (dec) => 01111111 (bin)

When you try to set a value that needs more than one byte to store then setting the lowest byte to a byte value happens:

+129 (dec) => 00000000 00000000 00000000 10000001  (int representation)

but 10000001 (bit) is -127 (dec) in byte representation of java type (as described above)

To have a better understanding of the overflow problem in Java, see the article: https://medium.com/@jeanvillete/java-numeric-overflow-underflow-d6b206f96d88

Matthew I.
  • 1,793
  • 2
  • 10
  • 21
4

129 is an integer literal and, since not followed by L or l, it is of type int. This has 32 bits (4 bytes):

0000 0000  0000 0000  0000 0000  1000 0001

When cast to byte 5.1.3. Narrowing Primitive Conversion is done which discard all but the lower 8 bits so the values fits into byte.

In your example we will end with just the lower 8 bits

1000 0001

since Java uses two's complement to represent byte, this number is considered negative since the highest bit is set.

If the value was 257, its binary representation would be:

0000 0000  0000 0000  0000 0001  0000 0001

converted to byte as:

0000 0001

or just +1 (highest bit not set).

Links:

Lino
  • 19,604
  • 6
  • 47
  • 65
user85421
  • 28,957
  • 10
  • 64
  • 87
-1

Beacause byte datatype is of 1 byte as per name. so, its range is -127 to +126 (i.e capacity is 256). Hence +129 cannot be stored in byte datatype. so +129 is truncated to -127.

Aman
  • 45
  • 1
  • 4