I know Java doesn't allow unsigned types, so I was wondering how it casts an integer to a byte. Say I have an integer a with a value of 255 and I cast the integer to a byte. Is the value represented in the byte 11111111? In other words, is the value treated more as a signed 8 bit integer, or does it just directly copy the last 8 bits of the integer?
8 Answers
This is called a narrowing primitive conversion. According to the spec:
A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.
So it's the second option you listed (directly copying the last 8 bits).
I am unsure from your question whether or not you are aware of how signed integral values are represented, so just to be safe I'll point out that the byte value 1111 1111 is equal to -1 in the two's complement system (which Java uses).

- 188,989
- 46
- 291
- 292
-
Yeah, I knew about two's complement. So, just to clarify, if I have a long type with a value of -1, and I cast it to an int, the result will be 65535? Thanks for the quick and detailed reply by the way. – LandonSchropp Mar 16 '10 at 22:29
-
1@helixed: No, it will also be -1 because it is also all ones. Anything in the range of the smaller type will be converted to the equivalent value. – Michael Myers Mar 16 '10 at 22:33
-
Oh wait, I see. Because of 2's complement, any negative value inside of the range of the smaller type will be padded with 1's (in binary) anyway, so the result will always retain its sign. Thanks again. – LandonSchropp Mar 16 '10 at 22:40
-
@helixed: Yes, that's correct. Once you get outside the range of the smaller type, it's effectively the same as overflow or underflow. – Michael Myers Mar 17 '10 at 15:25
int i = 255;
byte b = (byte)i;
So the value of be in hex is 0xFF but the decimal value will be -1.
int i = 0xff00;
byte b = (byte)i;
The value of b now is 0x00. This shows that java takes the last byte of the integer. ie. the last 8 bits but this is signed.

- 4,812
- 3
- 27
- 38
-
What can I google to learn more about braces type conversion trick? "(byte)i" – Roman Voronov May 17 '23 at 13:31
or does it just directly copy the last 8 bits of the integer
yes, this is the way this casting works

- 64,384
- 92
- 238
- 332
Byte is 8 bit. 8 bit can represent 256 numbers.(2 raise to 8=256)
Now first bit is used for sign. [if positive then first bit=0, if negative first bit= 1]
let's say you want to convert integer 1099 to byte. just devide 1099 by 256. remainder is your byte representation of int
examples
1099/256 => remainder= 75
-1099/256 =>remainder=-75
2049/256 => remainder= 1
reason why? look at this image https://i.stack.imgur.com/FYwqr.png

- 482
- 3
- 10
The following fragment casts an int to a byte. If the integer’s value is larger than the range of a byte, it will be reduced modulo (the remainder of an integer division by the) byte’s range.
int a;
byte b;
// …
b = (byte) a;

- 1,508
- 5
- 25
- 38
Just a thought on what is said: Always mask your integer when converting to bytes with 0xFF (for ints). (Assuming myInt was assigned values from 0 to 255).
e.g.
char myByte = (char)(myInt & 0xFF);
why? if myInt is bigger than 255, just typecasting to byte returns a negative value (2's complement) which you don't want.

- 87,898
- 29
- 167
- 228
-
-
3`char` is 16 bits in java. If you change your example to `byte` it doesn't work either and could still yield a negative value. You could mask with 0x7F, but then negative values wouldn't convert correctly. The good way to ensure you don't run into problems is probably to `assert -128 <= myInt && myInt <= 127;` – Philippe Beaudoin Mar 16 '10 at 22:17
-
@Philippe, thanks. I'm working very late so I wrote this too fast but yet, how big is `short` in java? – Buhake Sindi Mar 16 '10 at 22:27
-
1`short` is a signed two-byte type, while `char` is an unsigned two-byte type. – Michael Myers Mar 16 '10 at 22:30
-
I understand what you were trying to say, and it's good advice in general. In my specific case, I already know the value in the int is less than 255. – LandonSchropp Mar 16 '10 at 22:31
According to my understanding, you meant
Integer i=new Integer(2);
byte b=i; //will not work
final int i=2;
byte b=i; //fine
At last
Byte b=new Byte(2);
int a=b; //fine

- 179,855
- 19
- 132
- 245

- 21
- 5
for (int i=0; i <= 255; i++) {
byte b = (byte) i; // cast int values 0 to 255 to corresponding byte values
int neg = b; // neg will take on values 0..127, -128, -127, ..., -1
int pos = (int) (b & 0xFF); // pos will take on values 0..255
}
The conversion of a byte that contains a value bigger than 127 (i.e,. values 0x80 through 0xFF) to an int results in sign extension of the high-order bit of the byte value (i.e., bit 0x80). To remove the 'extra' one bits, use x & 0xFF; this forces bits higher than 0x80 (i.e., bits 0x100, 0x200, 0x400, ...) to zero but leaves the lower 8 bits as is.
You can also write these; they are all equivalent: int pos = ((int) b) & 0xFF; // convert b to int first, then strip high bits int pos = b & 0xFF; // done as int arithmetic -- the cast is not needed
Java automatically 'promotes' integer types whose size (in # of bits) is smaller than int to an int value when doing arithmetic. This is done to provide a more deterministic result (than say C, which is less constrained in its specification).
You may want to have a look at this question on casting a 'short'.