Convert Integer To Byte In Java

Convert Integer To Byte In Java | In this blog, we will see how to convert the integer data type to byte data type. Both int and byte are primitive data types having wrapper classes Integer and Byte respectively, but the only difference is their capacity to hold the values.

To convert Integer to Byte in Java we use three methods:-
1. By using the type casting.
3. By using Integer.byteValue()

The range of byte or Byte values is -27 to 27-1 Or, -128 to 127 whereas the range of int or Integer values is -231 to 231-1 Or, -2147483648 to 2147483647. See more:- Data types in Java. When we try to convert an int value like 128 which is greater than the max value of byte-range 127 then due to the circle it starts over and will be converted to the -128 byte value.

Java Convert Integer To Byte Using Type Casting

In this example, we are converting the int value to byte by type casting.

Java Program To Covert int To byte Primitive Value Using Type Casting

public class Main {
    public static void main(String[] args) {
        int num1 = 127;
        System.out.println("int value = " + num1);
        byte num2 = (byte) num1;
        System.out.println("byte value = " + num2);
        
        System.out.println();
        
        num1 = 130;
        System.out.println("int value = " + num1);
        num2 = (byte) num1;
        System.out.println("byte value = " + num2);
    }
}

Output:-

int value = 127
byte value = 127

int value = 130
byte value = -126

With a very similar approach, we can also convert wrapper class values from Integer to Byte type. The below program to convert Integer To Byte in Java demonstrates it.

Java Program To Covert Integer To Byte Wrapper class Value Using Type Casting

public class Main {
    public static void main(String[] args) {
        Integer n1 = 120;
        System.out.println("Integer value = " + n1);
        int value = n1; // convert Integer to int
        Byte byteValue = (byte) value;
        System.out.println("Byte value = " + byteValue);

        System.out.println();

        n1 = 135;
        System.out.println("Integer value = " + n1);
        value = n1;
        byteValue = (byte) value;
        System.out.println("Byte value = " + byteValue);
    }
}

Output:-

Integer value = 120
Byte value = 120

Integer value = 135
Byte value = -121

We can’t typecast the Integer wrapper class to the byte primitive value directly therefore first we need to convert the Integer wrapper class to a primitive int value. Later using that primitive int value we can type cast int to byte.

Convert Integer To Byte In Java Using Integer.byteValue()

The Integer.byteValue() is a built-in method available in the java library of integer class which allows us to convert the integer to byte directly and returns the signed value of byte, so if we want to get the signed value of byte then we can use byteValue() method.

Following is the syntax of byteValue() method:- public byte byteValue(). It returns the value of this Integer as a byte after a narrowing primitive conversion. It can be directly called on an Integer object.

In the below example, we have used Integer.byteValue() to convert the int primitive value to byte primitive value and also to convert the Integer wrapper value to Byte wrapper value.

Java Program To Covert Integer To Byte Wrapper class Value Using Integer.byteValue()

public class Main {
    public static void main(String[] args) {
        Integer num1 = 120;
        System.out.println("Integer value = " + num1);
        Byte num2 = num1.byteValue();
        System.out.println("Byte value = " + num2);

        System.out.println();

        num1 = 130;
        System.out.println("Integer value = " + num1);
        num2 = num1.byteValue();
        System.out.println("Byte value = " + num2);
    }
}

Output:-

Integer value = 120
Byte value = 120

Integer value = 130
Byte value = -126

Java Program To Covert int To byte Primitive Value Using Type Casting

public class Main {
    public static void main(String[] args) {
        int num1 = 250;
        System.out.println("int value = " + num1);
        Integer value = num1; // convert int to Integer
        byte num2 = value.byteValue();
        System.out.println("byte value = " + num2);
    }
}

Output:-

int value = 250
byte value = -6

Byte to Integer In Java

Here we are doing an unsigned conversion of an integer to a byte value.

public class Main {
    public static void main(String[] args) {
        int num1 = 130;
        System.out.println("int value = " + num1);
        byte num2 = (byte) num1;
        System.out.println("byte value = " + num2);
        num1 = num2 & 0xFF;
        System.out.println("int value = " + num1);
    }
}

Output:

int value = 130
byte value = -126
int value = 130

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *