Java Integer shortValue() 方法

2025 年 3 月 25 日 | 阅读 2 分钟

Java Integer shortValue() 方法在进行原始类型转换后,返回此 Integer 的值作为 short 类型。它重写了 Number 类的 shortValue() 方法。

语法

以下是 shortValue() 方法的声明

参数

数据类型参数描述
不适用不适用此方法不接受任何参数。

返回值

shortValue() 方法在转换为 short 类型后,返回此对象所表示的数值。

异常

不适用

兼容版本

Java 1.0 及以上版本

示例 1

输出

Value of s = 225
Value of s = -225

示例 2

输出

Enter Desired Number: 46546657
short Value is: 16097

示例 3

输出

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

	at myPackage.IntegerShortValueExample2.main(IntegerShortValueExample2.java:6)