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
- How to cast int value to byte value
- 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);
}
}
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);
}
}
int value 50
byte value 50
Happy Coding 😊