我使用ADT编写下面的代码,并且在比较两个整数时遇到一些问题。 这是源代码。
if(longCount == Tsleep)
Log.d("count", "=:"+longCount);
else if (longCount < Tsleep)
Log.d("count", "<:"+longCount);
else if(longCount > Tsleep)
Log.d("count", ">:"+longCount);
longCount++;
“Tsleep”的值为 396。 结果如下。
它似乎永远不会遇到“if(longCount==Tsleep)”语句。
有人知道这是怎么回事吗? 请帮助我,非常感谢。
最佳答案
如果这些变量是对象,则意味着它们是 Integer
s ,则 ==
适用于 [-128,127] 之间的数字。您可能想查看JLS欲了解更多(重要)信息:
If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
关于java - 整数比较问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21016368/