Java Convert Integer To Short and Vice-Versa

Java Convert Integer To Short & Convert Short To Integer Java | In this blog, we will discuss how to convert the integer or int into a short data type or Short object and its reverse i.e. the short/Short into an Integer/int data type.

The range of short data types is from -215 to 215-1 Or, -32768 to 32767 whereas the range of int data type is from -231 to 231-1 Or, -2147483648 to 2147483647. The int is a very commonly used data type in Java programming language but the short is a very rarely used data type.

Here we will be using two methods one to convert the short data type into an integer or int data type and the other to convert the integer or int into a short data type.

Java Convert Integer To Short

To convert an integer to a short value, the Java Integer class contains shortValue() method. It is an instance method that needs to be called on an Integer object and it will convert the given Integer object to a short primitive value.

Following is the method signature:- public short shortValue(); It returns the value of this Integer as a short after a narrowing primitive conversion.

Java Program to Convert Integer To Short primitive using shortValue() method

public class Main {
    public static void main(String[] args) {
        Integer number = 589;
        System.out.println("Integer value:  " + number);
        short value = number.shortValue();
        System.out.println("Short value: " + value);

        Integer number1 = (int) Math.pow(2, 19) + 100;
        System.out.println("Integer value:  " + number1);
        short value1 = number1.shortValue();
        System.out.println("Short value: " + value1);
    }
}

Output:-

Integer value: 589
Short value: 589
Integer value: 524388
Short value: 100

Java Program to Convert Integer To Short Object using shortValue() method

The return type of the shortValue() method is the short primitive data type. To convert a short primitive type to a Short Object we can take the help of the pre-defined method or auto-boxing and auto-unboxing feature. When we assign a short primitive value to a Short object then JVM internally wraps them from a primitive value to an Object.

public class Main {
    public static void main(String[] args) {
        Integer number = 589;
        System.out.println("Integer value:  " + number);

        // returns short and converted to Short object
        Short value = number.shortValue();

        // Or,
        // short val1 = number.shortValue();
        // Short value = val1; // Auto-boxing

        System.out.println("Short value: " + value);
        System.out.println("Class: " + value.getClass().getName());
    }
}

Output:-

Integer value: 589
Short value: 589
Class: java.lang.Short

Java Program to Convert int primitive To short primitive using shortValue() method

public class Main {
    public static void main(String[] args) {
        int num = 589;
        Integer number = num; // Auto-boxing
        System.out.println("Integer value:  " + number);
        short value = number.shortValue();
        System.out.println("Short value: " + value);
    }
}

Output:-

Integer value: 589
Short value: 589

Java Program to Convert int To Short Object using shortValue() method

public class Main {
    public static void main(String[] args) {
        int num = 589;
        Integer number = num; // Auto-boxing
        System.out.println("Integer value:  " + number);
        Short value = number.shortValue(); // Auto-boxing
        System.out.println("Short value: " + value);
    }
}

Output:-

Integer value: 589
Short value: 589

Convert Short To Integer Java

Similar to the Integer class, the Short class contains the intValue() method to convert short to Int value. It is an instance method that needs to be called on a Short object and it will convert the given Short object to an int value.

Following is the method syntax for the intValue() method:- public int intValue(). It returns the value of this Short as an int after a widening primitive conversion.

Convert Short To int in Java using the intValue() method

public class Main {
    public static void main(String[] args) {
        Short number = 569;
        System.out.println("Short value:  " + number);
        int value = number.intValue();
        System.out.println("Int value:  " + value);

        Short number1 = -143;
        System.out.println("Short value:  " + number1);
        int value1 = number1.intValue();
        System.out.println("Int value:  " + value1);
    }
}

Output:-

Short value: 569
Int value: 569
Short value: -143
Int value: -143

Convert Short To Integer in Java using the intValue() method

public class Main {
    public static void main(String[] args) {
        Short number = 569;
        System.out.println("Short value:  " + number);
        Integer value = number.intValue(); // Auto-boxing
        System.out.println("Int value:  " + value);
    }
}

Output:-

Short value: 569
Int value: 569

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *