Java - Number shortValue() Method



The Java Number shortValue() method is used to convert the value of the specified number into a short value.

The short data type is a Java primitive data type and is a 16-bit signed two's complement integer. The range of this short data type lies between -32,768 and 32,767 (both inclusive). This data type has almost same properties as byte except for its size; like this data type is used when there is a need to save memory while storing large arrays.

The default value and size of short data type is 0 and 2 bytes respectively.

Note − This conversion may involve rounding or truncation of the given number.

Syntax

Following is the syntax for Java Number shortValue() method

public abstract short shortValue()

Parameters

This method does not accept any parameters.

Return Value

This method returns the short value after conversion.

Example

The following example shows the usage of Java Number shortValue() method. We are assigning an int value to an Integer class reference variable. The method invoked on this object will convert this assigned value to its corresponding short value.

public class NumberShortVal {
   public static void main(String[] args) {
 
      // get a number as int
      Integer x = 892374;
 
      // print their value as short
      System.out.println("x as int: " + x + ", x as short: " + x.shortValue());
   }
}

Let us compile and run the program above, the output is displayed as follows −

x as int: 892374, x as short: -25130

Example

In this example, we are assigning a long value to a Long class reference variable. This long value is converted into a short value when the method is invoked on the Long class object created.

public class NumberShortVal {
   public static void main(String[] args) {
 
      // get a number as long
      Long x = (long) 6287209;
 
      // print their value as short
      System.out.println("x as long: " + x + ", x as short: " + x.shortValue());
   }
}

The output for the above program is given as follows −

x as long: 6287209, x as short: -4247

Example

In the following example, a float value is assigned to a Float class reference variable. The method is called on this Float object to convert the float value into a short value.

public class NumberShortVal {
   public static void main(String[] args) {

      // get a number as float
      Float x = 123456f;

      // print their value as short
      System.out.println("x as float: " + x + ", x as short: " + x.shortValue());
   }
}

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

x as float: 123456.0, x as short: -7616

Example

In the following program, we will see how this method will convert a double value, which is assigned to a Double class reference variable, into a short value.

public class NumberShortVal {
   public static void main(String[] args) {

      // get a number as double
      Double y = 9876d;

      // print their value as short
      System.out.println("y as double: " + y + ", y as short: " + y.shortValue());
   }
}

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

y as double: 9876.0, y as short: 9876

Example

In this example, the byte value assigned to the Byte class reference variable will be converted into a short value. This is done by invoking the method on the Byte class object created.

public class NumberShortVal {
   public static void main(String[] args) {
 
      // get a number as byte
      Byte x = 62;
 
      // print their value as short
      System.out.println("x as byte: " + x + ", x as short: " + x.shortValue());
   }
}

On compiling and running the given program, the output is displayed as follows −

x as byte: 62, x as short: 62
java_lang_number.htm
Advertisements