Calculating with the char variable in java

梦想的初衷 提交于 2019-11-26 11:39:45

问题


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..

and one of the calculations is:

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..

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..

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..

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 :)


回答1:


In Java, chars have a direct mapping to ints by UTF-16. For most common characters, though, casting a char value to an int yields its index on the ascii table. + isn't an operation on chars, but it is an operation for ints. 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.

As you can see in the table, '2' has a index of 50, thus 1 + 50 + 3 = 54.




回答2:


Characters are, in fact, numbers. Computer transforms a char into an integer using a character set (charset). 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). 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. 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.

Hope it clears things up a bit.



来源:https://stackoverflow.com/questions/25593074/calculating-with-the-char-variable-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!