[英]When I set this to a variable, it changes the value. Does anyone know why?
int temp = a1.charAt(num);
System.out.println(a1.charAt(num));
System.out.println(temp);
(a1 is a string that contains 6 8 1 5...) (a1 是一个包含 6 8 1 5... 的字符串)
(num is 0) (数字为 0)
the output is: output 是:
6 6
54 54
(int)'6' == 54
'6' == (char)54
'6' == (char)0x36
System.out.println
has different overloads for parameters of type int
(prints the numeric/ordinal representation of the value) and for parameters of type char
(prints the character representation of the ordinal value). System.out.println
对int
类型的参数(打印值的数字/顺序表示)和char
类型的参数(打印顺序值的字符表示)具有不同的重载。
It is because the .chatAt()
method returns a char and you are storing it in an integer variable.这是因为.chatAt()
方法返回一个 char 并且您将它存储在 integer 变量中。 Hence, it is stored as it's ASCII value.因此,它被存储为它的 ASCII 值。
6 in ASCII is 54. ASCII 中的 6 是 54。
Link - https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html链接 - https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
ASCII value of 6 is 54. ASCII 值 6 是 54。
When you a1.charAt(0) it prints character 6 from the string = 6815.当您使用 a1.charAt(0) 时,它会从字符串 = 6815 中打印字符 6。
But when you assign char 6 to int, then it prints its equivalent ASCII value, which is 54.但是当您将 char 6 分配给 int 时,它会打印出其等效的 ASCII 值,即 54。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.