Convert byte to int in Java

Tags: int byte Integer Convert

In this Java core tutorial, we learn how to convert byte value into int value in Java via different solutions.

Assign byte variable to int variable

ConvertByteToIntExample1.java

public class ConvertByteToIntExample1 {
    public static void main(String[] args) {
        byte value1 = 98;
        int value2 = value1;

        System.out.println("byte value " + value1);
        System.out.println("int value " + value2);
    }
}
The output is:
byte value 98
int value 98

Using Integer.valueOf() method to instantiate Integer value from byte value

ConvertByteToIntExample2.java

public class ConvertByteToIntExample2 {
    public static void main(String[] args) {
        byte value1 = 100;
        int value2 = Integer.valueOf(value1);

        System.out.println("byte value " + value1);
        System.out.println("int value " + value2);
    }
}
The output is:
byte value 100
int value 100

Using Byte.intValue() method to get int value of a Byte object

ConvertByteToIntExample3.java

public class ConvertByteToIntExample3 {
    public static void main(String[] args) {
        Byte value1 = 78;
        int value2 = value1.intValue();

        System.out.println("byte value " + value1);
        System.out.println("int value " + value2);
    }
}
The output is:
byte value 78
int value 78

Happy Coding 😊