当我执行 Integer.value Of('a') 时 它返回 97,即字符 a 的 ASCII 值。
我想知道 valueOf 方法中 char 何时转换为 ascii 值?
最佳答案
发生在你身上的是内部类型转换。
所以你的代码相当于这样:
char a = 'a';
int aint = (int)a;
Integer aInteger = Integer.valueOf(aint);
System.out.println(aInteger.toString());
引用oracle java教程:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
因此变量 a 保存与字符“a”(97) 等效的 unicode
接下来发生的是扩大操作 https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
5.1.2. Widening Primitive Conversion
[...]
A widening conversion of a char to an integral type T zero-extends the representation of the char value to fill the wider format.
因此您的 char(16 位)将被扩展为 int(32 位)。
这个 int 就是被输入到 Integer.valueOf() 中的内容
希望能回答您的问题。
托比
关于java - 整数如何自动将字符转换为ASCII值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45162300/