printf displays different values for the same variable

穿精又带淫゛_ 提交于 2019-12-30 05:54:12

问题


I found this code online and when compiled it prints 5 and 10:

int numbеr = 5;
int number = 10;
System.out.printf("Number one is %d and number two is %d.", numbеr, number);

How can this work ?!


回答1:


Java supports Unicode characters, one or more of the letters in one of the "numbers" variable is unicode letter from different alphabet, you can check this by copy-pasting both of those names and trying to do this:

System.out.println("numbеr".equals("number"));

They seem same to the naked eye, but they are not, your IDE will show false in the console as the evaluation result. Try it yourself.

EDIT:

The letter 'e' is different, outputs of casting it to int are 101 and 1077, if you do google search for "unicode 1077" the following link is one of the many the search engine will throw at you:

http://www.codetable.net/decimal/1077

As you can see it says it's Cyrillic lowercase letter 'e'.



来源:https://stackoverflow.com/questions/47625531/printf-displays-different-values-for-the-same-variable

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