Java convert short to byte
In this Java core tutorial we learn how to convert a short value into a byte value with different solutions in Java programming language.
Table of contents
How to cast short to byte value
In this first solution to convert a short to byte value we just simple cast a short variable to a byte variable as the example Java code below.
ConvertShortToByteExample1.java
public class ConvertShortToByteExample1 {
public static void main(String... args) {
short shortValue = 67;
byte byteValue = (byte)shortValue;
System.out.println("short value: " + shortValue);
System.out.println("byte value: " + byteValue);
}
}
short value: 67
byte value: 67
Using Short.byteValue() method
In this second solution, we use the Short.byteValue() method to return a byte value from a given Short object as the following Java code.
ConvertShortToByteExample2.java
public class ConvertShortToByteExample2 {
public static void main(String... args) {
Short shortValue = 69;
byte byteValue = shortValue.byteValue();
System.out.println("short value: " + shortValue);
System.out.println("byte value: " + byteValue);
}
}
short value: 69
byte value: 69
Happy Coding 😊
Related Articles
Java convert short to BigInteger