简体   繁体   English

用Java中的char变量进行计算

[英]Calculating with the char variable in java

I have an assignment for school, and one of the tasks is to explain a lot of tiny calculations and explaining why java gives you the output it gives you.. 我有一个学校的作业,任务之一是解释很多小的计算,并解释为什么Java给您输出它给您的输出。

and one of the calculations is: 计算之一是:

1 + '2' + 3 1 +'2'+ 3

which for me gives a lexical error, as the teacher used the wrong "apostrophes" for my system, but I've talked to other fellow students and they told me they got an actual output, so I started reading about it, and found out that it is supposed to signify a char variable, and I also found out about the system specific types, so I changed the signs to work for my system, and now I get the answer 54 .. 这对我来说是一个词法错误,因为老师在我的系统上使用了错误的“撇号”,但是我与其他同学交谈,他们告诉我他们得到了实际的输出,因此我开始阅读并发现了认为它应该表示一个char变量,并且我还了解了系统的特定类型,因此我更改了适用于系统的符号,现在得到了答案54 ..

and I cannot see the logic in it, and I've tried to google adding/calculating/math with char variables, and have found nothing that explains it well.. 而且我看不到其中的逻辑,而且我尝试用char变量在Google中添加/计算/运算,但没有找到任何能很好解释它的方法。

So I turn to you, the people of coding, that I one day might be a part of to help me understand the logic of this.. 因此,我向编码人员求助,希望有一天我可以参与其中,以帮助我理解其逻辑。

this started out as a homework assignment that I probably could have gotten through by just answering that it gives a lexical error because my compiler doesn't understand the symbol, but now it's peaked my curiosity, and I really want to know how java manages to get this answer.. 这最初是作为一项家庭作业,我可能可以通过回答它会产生一个词法错误来解决,因为我的编译器不理解该符号,但是现在它达到了我的好奇心,我真的很想知道java如何管理得到这个答案..

thank you for any help on the matter! 感谢您对此事的任何帮助! :) :)

I can see that I couldn't make a 'homework' tag, so I hope it's okay that I put it here :) 我可以看到我无法制作“作业”标签,所以我希望可以将它放在这里:)

In Java, char s have a direct mapping to int s by UTF-16. 在Java中, char通过UTF-16直接映射到int For most common characters, though, casting a char value to an int yields its index on the ascii table. 但是,对于大多数常见字符,将char值转换为int在ascii表上产生其索引。 + isn't an operation on chars, but it is an operation for ints. +不是对char的运算,而是对int的运算。 Therefore, java is taking the 2 and after thinking "I can't add this", realizes it can add it if it casts it to an int. 因此,java采用2并考虑“我不能添加”,意识到如果将其强制转换为int,则可以添加。 ASCII表

As you can see in the table, '2' has a index of 50, thus 1 + 50 + 3 = 54. 如表中所示,“ 2”的索引为50,因此1 + 50 + 3 = 54。

Characters are, in fact, numbers. 字符实际上是数字。 Computer transforms a char into an integer using a character set (charset). 计算机使用字符集(字符集)将char转换为整数。 Charsets are tables which bind specified character to the specified integer, so your computer can transform it to binary and store it in memory. 字符集是将指定字符绑定到指定整数的表,因此您的计算机可以将其转换为二进制并将其存储在内存中。 Later when needed, that number can get transformed back to character. 以后需要时,该数字可以转换回字符。 Your compiler uses Unicode (a superset of ASCII, thanks Tom Blodget), so character '2' is actually decimal integer 50 (more info: http://unicode-table.com/en/ , note that table uses hex numbers, so hex 32 is decimal 50). 您的编译器使用Unicode(感谢Tom Blodget,是ASCII的超集),因此字符“ 2”实际上是十进制整数50(更多信息: http : //unicode-table.com/en/ ,请注意表使用十六进制数字,因此十六进制32是十进制50)。 Since addition by default returns an integer, the character gets transformed and added, but never actually transformed back to its original form. 由于默认情况下加法会返回整数,因此字符会进行转换和添加,但实际上从未转换回其原始形式。

As for why it gives in error, it's probably because Java differs between types and your compiler (or whatever's throwing the error) is more strict. 至于为什么会出错,可能是因为Java在类型之间有所不同,并且您的编译器(或引发错误的原因)更加严格。 Don't take my word for that, you should probably look in the standard and your implementation. 不要相信我,您应该考虑标准和实现。 In C for example, that addition would always succeed. 例如,在C语言中,添加总是会成功。

Hope it clears things up a bit. 希望它能解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM