Convert Int to Byte in Java

Mohammad Irfan Dec 21, 2022 Nov 04, 2021
  1. Int to Byte Conversion in Java
  2. Int to Byte Conversion and Vice Versa in Java
  3. Int to Byte Unsigned Conversion in Java
  4. Int to Byte Conversion and Vice Versa Using Java 8
  5. Int to Byte Using byteValue() Method in Java
Convert Int to Byte in Java

This tutorial introduces how to convert int to the byte in Java.

In Java, int and byte both are primitive types and used to store numeric values. Both are used to store signed, and unsigned values but have different storage ranges. The byte range is -128 to 127 and the int range is -2,147,483,648 to 2,147,483,647. So, clearly, we can see that int can store a large value than byte type.

While converting int to byte, some data gets lost due to memory. Let’s see some examples.

Int to Byte Conversion in Java

In this example, we convert int to byte by casting and see if the int value exceeds out of byte’s range. If it does, then it’s converted into a negative value. It means if a value is larger than the byte range, then it gets converted to negative. So, when we convert a large value to byte type, we get a different result than int type.

public class SimpleTesting{
	public static void main(String[] args){
		int a = 127;  // byte max positive range
		System.out.println("int value = "+a);
		byte b = (byte) a;
		System.out.println("byte value = "+b);
		a = 130; // Out of positive byte range
		System.out.println("int value = "+a);
		b = (byte) a;
		System.out.println("byte value = "+b);
	}
}

Output:

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

Int to Byte Conversion and Vice Versa in Java

To convert an int type to a byte type, we need to use explicit typecasting. However, to convert the byte to int, we don’t need any explicit casting. Java does this implicitly. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		int a = 230;
		System.out.println("int value = "+a);
		byte b = (byte) a;
		System.out.println("byte value = "+b);
		a = b;
		System.out.println("int value = "+a);
	}
}

Output:

int value = 230
byte value = -26
int value = -26

Int to Byte Unsigned Conversion in Java

We did sign conversion in all the above examples, but if you want unsigned conversion, then use the code below. Here, we used the & 0xFF code, along with the int value, to get an unsigned conversion. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		int a = 230;
		System.out.println("int value = "+a);
		byte b = (byte) a;
		System.out.println("byte value = "+b);
		a = b & 0xFF; 
		System.out.println("int value = "+a);
	}
}

Output:

int value = 230
byte value = -26
int value = 230

Int to Byte Conversion and Vice Versa Using Java 8

If you are working with Java 8, or a higher version, then use the toUnsignedInt() method of the Byte class to get an unsigned conversion. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		int a = 230;
		System.out.println("int value = "+a);
		byte b = (byte) a;
		System.out.println("byte value = "+b);
		a = Byte.toUnsignedInt(b);
		System.out.println("int value = "+a);
	}
}

Output:

int value = 230
byte value = -26
int value = 230

Int to Byte Using byteValue() Method in Java

We can also use the byteValue() method of the Integer class to get the byte value after conversion. This method returns a signed value. So, use it only if you want to get a signed result. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		int a = 230;
		System.out.println("int value = "+a);
		Integer i = a;
		byte b = i.byteValue();
		System.out.println("byte value = "+b);		
	}
}

Output:

int value = 230
byte value = -26

Related Article - Java Int

Related Article - Java Byte