When an integer value is converted into a byte, Java cuts-off the left-most 24 bits.
We will be using bitwise AND to mask all of the extraneous sign bits.
Here is an illustration of a Java Program that converts an integer to byte.
import java.util.Scanner;
public class Integer_Conversion
{
public static void main(String[] args)
{
int a;
byte b;
Scanner s = new Scanner(System.in);
System.out.print("Enter any integer:");
a = s.nextInt();
b = (byte) a;
System.out.println("Conversion into byte:"+b);
}
}
Output:
Enter any integer: 100
Conversion into byte:100
Hope it helps!!
If you need to know more about Java, join our Java online course today.
Thank you