2

I write the following sample code:

public static void main(String[] args) throws Exception
{
      byte number_1 =  127;
      byte number_2 =  (byte) 128;
      System.out.println("number_1 = " + number_1);
      System.out.println("number_2 = " + number_2);
}

I get the following result in output:

number_1 = 127
number_2 = -128

I know range of a byte data type( -128 to 127). Is my sample is correct? What happened? Is there a two's complement operation? I don't understand this behavior.

Sam
  • 6,770
  • 7
  • 50
  • 91
  • That's 2's complement. A `byte` is 8 bits, from 00000000 to 11111111. Since a `byte` is signed, any value larger than 01111111 (ie, any value with the high-order bit set to 1) will be considered to be negative. – Hot Licks Nov 05 '13 at 12:01

5 Answers5

12

Because one byte can hold upto -128 to 127 only, This is expected behavior of overflow

Check with this loop

for(int index = 0 ; index < 258 ; index ++ ){
  System.out.println((byte)index);
}

number line

Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Thank for your answer but I know range of a `byte` data type, I don't understand this behavior. – Sam Jun 25 '12 at 05:53
  • 3
    @MJM its not clear what you dn't understand. Think of a clock, the hour after 12 (the last) is 1 (the first) and the hour before 1 (the first) is 12 (the last) – Peter Lawrey Jun 25 '12 at 05:59
  • @Peter nice example, MJM check the updated answer its simple overflow – jmj Jun 25 '12 at 06:01
  • @PeterLawrey your example is very nice. – Sam Jun 25 '12 at 06:04
  • 2
    @MJM A byte can only keep the lowest 8-bits of the result. This is what causes underflows (wrapping from negative to positive) and overflows (wrapping from positive to negative) because a top bits have been dropped. – Peter Lawrey Jun 25 '12 at 06:08
4

This is because of byte range . A byte can store values from -128 to 127 only.

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
1

From the official documentation on primitive datatypes :

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).

To respond your question : How to Set greater than 127 int byte variable in java ?

The best way to represent an unsigned byte is to use a signed integer, because the Java VM represents bytes as 32 bits, you're not saving anything by using a byte.

aleroot
  • 71,077
  • 30
  • 176
  • 213
1

You're seeing the effect of a narrowing primitive conversion: casting the integer literal 128 to a byte results all all but the last byte being thrown out. The last byte of an integer has value 10000000, which when interpreted in two's complement as a one-byte value comes out to -128.

In contrast, the same value interpreted as a four-byte value filled with zeroes on the left, i.e. 00000000 00000000 00000000 10000000, equals 128.

oksayt
  • 4,333
  • 1
  • 22
  • 41
0

Because one byte can hold upto -128 to 127 only, when you transform a int which is more than 127 or less than -127, the java compiler makes the change automatically. In fact, the following statement

byte number_2 =  (byte) 128;

has been changed to

byte number_2 =  (byte) -128;

Once you check out the bytecode using javap, you will find it.

xiaoyu
  • 1