Java - Short intValue() Method



The Java Short intValue() method retrieves the equivalent integer value of the given short value after a widening primitive conversion (converting smaller data type to a bigger data type). It is an inherited member of the Number class. The data type int can store whole numbers in the range of -2,147,483,648 to 2,147,483,647.

Briefly, using this method, a Short object value is converted into a primitive integer value.

For example, assume we have a short value ’87’. The int value of the given short value is ‘87’.

Short d1 = 87
Int value = 87

Syntax

Following is the syntax for Java Short intValue() method −

public int intValue()

Parameters

This method does not accept any parameter.

Return Value

This method returns the short value represented by this object converted to type int.

Example

Following is an example to show the usage of Java Short intValue() method. In here we are creating a Short object with a positive value. Thereafter, printing the integer value of it −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      /*
      * returns the short value represented by this object
      * converted to type int
      */
      Short obj = new Short("32");
      int i = obj.intValue();
      System.out.println("Value = " + i);
      obj = new Short("30");
      i = obj.intValue();
      System.out.println("Value = " + i);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Value = 32
Value = 30

Example

Following is another example of this function −

import java.util.Scanner;
public class ShortDemo {
   public static void main(String[] args) {
      Short value1 = -35;
      Short value2 = -84;
      Short value3 = 987;
      System.out.println("Product of negative numbers = " + value1 * value2);
      System.out.println("Product of negative numbers = " + value1 * value3);
      int s = value1.intValue() * value2.intValue();
      System.out.println("Product of integer values = " + s);
   }
}

Output

Following is an output of the above code −

Product of negative numbers = 2940
Product of negative numbers = -34545
Product of integer values = 2940

Example

In the example below short variable ‘value’ is created. Then the MAX_VALUE is assigned to this variable. Thereafter, we retrieve its corresponding integer value −

public class ShortDemo {
   public static void main(String[] args) {
      Short value = Short.MAX_VALUE;
      System.out.println("The short MAX_VALUE is: " + value);
      
      // it returns int type value
      int result = value.intValue();
      System.out.println("The int value of short is: " + result);
   }
}

Output

On executing the program above, the output is obtained as follows −

The short MAX_VALUE is: 32767
The int value of short is: 32767

Example

In the example below short variable ‘value’ is created. Then the MIN_VALUE is assigned to this variable. Thereafter, we retrieve its corresponding integer value −

public class ShortDemo {
   public static void main(String[] args) {
      Short value = Short.MIN_VALUE;
      System.out.println("The short MIN_VALUE is: " + value);
      
      // it returns int type value
      int result = value.intValue();
      System.out.println("The int value of short is: " + result);
   }
}

Output

While executing the above code, we get the following output −

The short MIN_VALUE is: -32768
The int value of short is: -32768
Advertisements