Convert Long Values into Byte Using Explicit Casting in Java
In Java, a byte can contain only values from -128 to 127, if we try to cast a long value above or below the limits of the byte then there will be a precision loss.
1. byte: The byte data type is an 8-bit signed two’s complement integer.
byte varName; // Default value 0
1 byte (8 bits) : -128 to 127
2. long: The long data type is a 64-bit two’s complement integer.
long varName; // Default value 0
8 byte (64 bits): -9223372036854775808 to 9223372036854775807
Example 1: In limits
Example 2: Out of limits
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.