Convert int to byte in Java

Tags: int byte Integer Convert

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

Table of contents

  1. How to cast int value to byte value
  2. Using Integer.byteValue() method to get byte value from Integer object

How to cast int value to byte value

ConvertIntToByteExample1.java

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

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

Using Integer.byteValue() method to get byte value from Integer object

ConvertIntToByteExample2.java

public class ConvertIntToByteExample2 {
    public static void main(String[] args) {
        Integer value1 = Integer.valueOf(50);
        byte value2 = value1.byteValue();

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

Happy Coding 😊

Convert byte to int in Java

Convert int to String in Java

Convert int to float in Java

Convert int to long in Java

Convert int to short in Java

Convert int to double in Java