1. 简介
getNumericValue 获得字符的int值。
2. 实践
import java.lang.*;
public class CharacterDemo {
public static void main(String[] args) {
// create 2 character primitives ch1, ch2
char ch1, ch2;
// assign values to ch1, ch2
ch1 = 'j';
ch2 = '4';
// create 2 int primitives i1, i2
int i1, i2;
// assign numeric values of ch1, ch2 to i1, i2
i1 = Character.getNumericValue(ch1);
i2 = Character.getNumericValue(ch2);
String str1 = "Numeric value of " + ch1 + " is " + i1;
String str2 = "Numeric value of " + ch2 + " is " + i2;
// print i1, i2 values
System.out.println( str1 );
System.out.println( str2 );
}
}
Numeric value of j is 19
Numeric value of 4 is 4
方法返回特定字符的int值。字母 A-Z, a-z ,以及全宽变体的数值从10到35. 该方法和Unicode表示独立。
如何字符没有数值,返回 -1,如何数值不能表示成正数,返回 -2.
参考: