Casting long to byte with intermediate int

Report a typo

Imagine you're working on a Java program and you have a large numerical value stored in a data type that can handle larger numbers. You need to store this value in a less capable data type, but you need to make sure it fits - otherwise, data may be lost. Given this, can you fill in the blanks in the provided code to properly cast the long value to an int, and then the int to a byte?

Fill in the gaps with the relevant elements
public class TypeCastingExample {
    public static void main(String[] args) {
         longValue = 300;
         intValue = (int) longValue;
         byteValue = (byte) intValue;
        System.out.println("Long value: " + longValue);
        System.out.println("Int value: " + intValue);
        System.out.println("Byte value: " + byteValue);
    }
}
longbyteint
___

Create a free account to access the full topic