Java Integer.shortValue() – Examples
In this tutorial, we will learn about Java Integer.shortValue() method, and learn how to use this method to get the equivalent short value for this Integer object, with the help of examples.
shortValue()
Integer.shortValue() returns the value of this Integer as a short after a narrowing primitive conversion.
Syntax
The syntax of shortValue() method is
Integer.shortValue()
Returns
The method returns value of type short.
Example 1 – shortValue()
In this example, we will take an Integer object and find its equivalent short value using Integer.shortValue() method.
Java Program
public class Example {
public static void main(String[] args){
Integer integer = 20;
short result = integer.shortValue();
System.out.println("Result of shortValue() = " + result);
}
}
Output
Result of shortValue() = 20
Example 2 – shortValue() – Narrowed Value
In this example, we will take an Integer object with value out of range for a short value.
Java Program
public class Example {
public static void main(String[] args){
Integer integer = 1985781260;
short result = integer.shortValue();
System.out.println("Result of shortValue() = " + result);
}
}
Output
Result of shortValue() = -25076
Conclusion
In this Java Tutorial, we have learnt the syntax of Java Integer.shortValue() method, and also how to use this method with the help of Java example programs.