Java convert short to long
In this Java core tutorial we learn how to convert a short value into a long value with different solutions in Java programming language.
Table of contents
- Assign short variable to a long variable in Java
- Using Long.valueOf() method
- Using Short.longValue() method
Assign short variable to a long variable in Java
In this first solution to convert short value into long value we just need to assign the short variable directly to the long variable as the following Java code.
ConvertShortToLongExample1.java
public class ConvertShortToLongExample1 {
public static void main(String... args) {
short shortValue = 67;
long longValue = shortValue;
System.out.println("short value: " + shortValue);
System.out.println("long value: " + longValue);
}
}
short value: 67
long value: 67
Using Long.valueOf() method
In this second solution, we use the Long.valueOf(long l) static method to instantiate a new Long object from a given short value as the example Java code below.
ConvertShortToLongExample2.java
public class ConvertShortToLongExample2 {
public static void main(String... args) {
short shortValue = 89;
Long longValue = Long.valueOf(shortValue);
System.out.println("short value: " + shortValue);
System.out.println("long value: " + longValue);
}
}
short value: 89
long value: 89
Using Short.longValue() method
In this third solution, we use the Short.longValue() method to convert a given Short object into long value as the following Java code.
ConvertShortToLongExample3.java
public class ConvertShortToLongExample3 {
public static void main(String... args) {
Short shortValue = 69;
long longValue = shortValue.longValue();
System.out.println("short value: " + shortValue);
System.out.println("long value: " + longValue);
}
}
short value: 69
long value: 69
Happy Coding 😊
Related Articles
Java convert short to BigInteger