java - 整数比较问题

标签 java android

我使用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。 结果如下。

enter image description here

它似乎永远不会遇到“if(longCount==Tsleep)”语句。

有人知道这是怎么回事吗? 请帮助我,非常感谢。

最佳答案

如果这些变量是对象,则意味着它们是 Integers ,则 == 适用于 [-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/

相关文章:

java - 如何将 JProfiler 连接到在本地主机上运行的应用程序?

java - 从 POST 请求中检索图像

java - 如何以简洁的方式收集抛出异常的 Java 流操作的结果?

android - 在 fragment 内的异步任务中显示()进度对话框

java - 如何制作方形框架布局?

java - Android 倒计时器 - 循环使用列表值重新启动计时器的 ArrayList

java - Selenium : How to stop geckodriver process impacting PC memory, 没有调用 driver.quit()?

java - 访问线程以从另一个方法通知它(Android 应用程序)

java - 使用平面 4 :2:0 YUV full scale pixel format 将图像从字节数组转换为位图

Android Studio 呈现交互式内容的错误