简体   繁体   English

当我将其设置为变量时,它会更改值。 有谁知道为什么?

[英]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

cf.参看。 ASCII ASCII

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.printlnint类型的参数(打印值的数字/顺序表示)和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.

相关问题 当我设置setRGB的值时,getRGB返回不同的值。 为什么? - When I set a value of setRGB, getRGB returns a different value. Why? 当变量具有空值时,我的应用程序停止。 为什么? - My app stops when a variable has null value. why? 有谁知道为什么我得到这个IllegalMonitorStateException? - Does anyone know why I am getting this IllegalMonitorStateException? 调用我创建的用于减小变量值的方法不会影响该值。 我该如何解决? - Invoking a method I created to decrease the value of a variable does not have an effect to the value. How can I solve this? 有谁知道为什么这不能正常工作? - Does anyone know why this isnt working properly? 有谁知道为什么我会收到以下错误?:令牌上的语法错误,构造错误的位置 - Does anyone know why I am getting the following error?: Syntax error on token(s), misplaced construct(s) 我的Paint方法运行了两次,我不明白为什么。我怎么能解决这个问题,有谁知道为什么会这样? - My Paint method is running twice and I have no idea why. How can I fix this, and does anyone know why this is happening? 有谁知道如何使用 JNI-Registry (com.ice.jni.registry) 为密钥设置默认注册表值? - Does anyone know how to set the default registry value for a key using JNI-Registry (com.ice.jni.registry)? 有谁知道为什么我的 else if 语句不起作用? - Does anyone know why my else if statements are not working? 有谁知道为什么“File.mkdirs()”的代码结果被忽略? - Does anyone know why the code Result of 'File.mkdirs()' is ignored?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM