int to byte Java

int to byte Java


After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast int to byte.

The byte takes 1 byte of memory and int takes 4 bytes of memory. Assigning 4 bytes of memory to 1 byte of memory requires explicit casting.

byte –> short –> int –> long –> float –> double

The left-side value can be assigned to any right-side value and is done implicitly. The reverse like int to byte requires explicit casting.

Examples of implicit casting

int x = 10;
long y = x;

byte x = 10;
int y = x;

Following program on int to byte explains explicit casting, observe Java style of explicit casting.


int to byte
Output screenshot of int to byte Java

byte b1 = i1;

The above statement raises a compilation error "possible loss of precision".

byte b1 = (byte) i1;

The int i1 is explicitly type casted to byte b1. Observe, the syntax of explicit casting. On both sides, it should be byte only.

View all for 65 types of Conversions

Leave a Reply

Your email address will not be published. Required fields are marked *