Java - Short shortValue() Method



The Java Short shortValue() method retrieves the equivalent short value of the given Short after a narrowing primitive conversion which means conversion of a higher data type to a lower data type. The data type short can store whole numbers in the range of -32,768 to 32,767.

Generally, it is an Unboxing method. Although autoboxing was introduced with java 5 which means that the conversion is now done automatically, it is important to understand the concept of Boxing and Unboxing.

  • Unboxing is the process of converting Short to short

  • Boxing is the process of converting short to Short.

For example, assume we have a short value ’876’. The equivalent Short of the given short value is ‘876’.

Short s = 876
shortValue s1 = 876 // corresponding short value

Syntax

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

public short shortValue()

Parameters

This method does not accept any parameter.

Return Value

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

Example

Following is an example to show the usage of Java Short shortValue() method. Here we are creating a Short object 'obj' and assigning a positive value to it. Thereafter, displaying the short value by invoking the method on this Short object −

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

Output

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

Value = 35
Value = 2

Example

In the code given below try-catch statement is used to return the short value of the given Short −

import java.util.Scanner;
public class ShortDemo {
   public static void main(String[] args) {
      try {
         short value = -739;
         Short s = value;
         short x = s.shortValue();
         System.out.println("The corresponding short Value is = " + x);
      } catch (Exception e) {
         System.out.println("Error: not a valid short value");
      }
   }
}

Output

Following is an output of the above code −

The corresponding short Value is = -739

Example

The shortValue() method returns an error when the method is invoked on the Short objects having decimal value and a string as shown in the following example −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      short s = 8.3;
      Short x = new Short(s);
      
      // returning the short value of Short
      short value = x.shortValue();
      
      // Printing the short value
      System.out.println("The short value is: " + value);
      short s1 = "87";
      Short y = new Short(s1);
      
      // returning the short value of Short
      short value1 = y.shortValue();
      // Print the short value
      System.out.println("The short value is: " + value1);
   }
}

Exception

Output of the above code is as follows −

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
      Type mismatch: cannot convert from double to short
      Type mismatch: cannot convert from String to short

      at ShortDemo.main(ShortDemo.java:7)
java_lang_short.htm
Advertisements