Saturday, October 18, 2025
HomeLanguagesJavaShort shortValue() Method in Java

Short shortValue() Method in Java

The shortValue() is an inbuilt method of the Short class in Java and is used to return the Short value of this value.

Syntax:

public short shortValue()

Parameters: The method does not take any parameter.

Return Value: The method returns the numeric value represented by this object after conversion to type short.

Below programs illustrate the Short.shortValue() method:

Program 1:




// Java program that demonstrates
// Short.shortValue() method
  
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        short svalue = 21;
        Short sh_obj = new Short(svalue);
  
        // It will return the short value of Short
        short sh_Value = sh_obj.shortValue();
  
        // Printing short value
        System.out.println("The short value of the given Short is = "
                                                          + sh_Value);
    }
}


Output:

The short value of the given Short is = 21

Program 2:




// java program that demonstrates
// Short.shortValue() method
  
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        short svalue = -19;
        Short sh_obj = new Short(svalue);
  
        // It will return the short value of Short
        short sh_Value = sh_obj.shortValue();
  
        // Printing short value
        System.out.println("The short value of the given Short is = " 
                                                          + sh_Value);
    }
}


Output:

The short value of the given Short is = -19

Program 3:
Note: It returns an error message when a decimal value and string is passed as an argument.




// Java program that demonstrates
// Short.shortValue() method
  
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        short svalue = 9.6;
        Short sh_obj = new Short(svalue);
  
        // It will return the short value of Short
        short sh_Value = sh_obj.shortValue();
  
        // Printing short value
        System.out.println("The short value of the given Short is = "
        + sh_Value);
        short svalue2 = "61";
        Short sh_obj2 = new Short(svalue2);
  
        // It will return the short value of Short
        short sh_Value2 = sh_obj2.shortValue();
  
        // Printing short value
        System.out.println("The short value of the given Short is = " +
        sh_Value2);
    }
}


Output:


prog.java:10: error: incompatible types: possible lossy conversion from double to short
    short svalue = 9.6;
                   ^
prog.java:18: error: incompatible types: String cannot be converted to short
    short svalue2 = "61";
                    ^
2 errors
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS