Casting an int to byte discards everything above the lowest 8 bits, so the result depends only on those bits and can flip sign.
int i = 300;
byte b = (byte) i;
int j = -1;
byte b2 = (byte) j;
System.out.println("300 in binary: " + Integer.toBinaryString(i));
System.out.println("(byte) 300 = " + b);
System.out.println("(byte) -1 = " + b2);
300 in binary: 100101100
(byte) 300 = 44
(byte) -1 = -1
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27