Java convert short to long

Tags: short 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

  1. Assign short variable to a long variable in Java
  2. Using Long.valueOf() method
  3. 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);
    }
}
The output as below.
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);
    }
}
The output as below.
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);
    }
}
The output as below.
short value: 69
long value: 69

Happy Coding 😊

Java convert short to byte

Java convert short to BigInteger

Java convert short to BigDecimal

Java convert short to char

Java convert long to short

Java convert char to short

Java convert byte to short