Java Character getNumericValue() 方法

2024年12月12日 | 阅读 2 分钟

character 类的 getNumericValue() 方法返回指定字符的 int 值。如果该字符没有 int 值,则返回 -1。如果该字符具有无法表示为非负整数的数值,则返回 -2。

语法

参数

上述方法只需要一个参数

codePoint 是要转换的字符(Unicode 码点)。

返回值

getNumericValue(int codePoint) 方法返回字符的数值,作为一个非负 int 值。如果该字符具有数值但该数值无法表示为非负 int 值,则此方法返回 -2。如果该字符没有数值,则此方法返回 -1。

示例 1

输出

The character 'n' has a numeric value represented as: 23

示例 2

输出

Numeric value for code point obj1 is 33
Numeric value for code point obj2 is -1

示例 3

输出

The character 'n' has a numeric value represented as: 23
The character ' ' has a numeric value represented as: -1
The character '6' has a numeric value represented as: 6