2015-05-28 33 views
0

这将是一个简单的问题(或者至少我认为)。我必须将一个Long对象与一个长基元进行比较。两个多头之间的比较不能按预期工作

我的代码简化:

long primitiveLong = 285825645621; 
Long objectLong = new Long(285825645621); 

if(primitiveLong == objectLong.longValue()){ 
    System.out.print("They are equals"); 
} 

我预计它会工作,但事与愿违,有人可以解释我为什么?

在此先感谢。

编辑

好吧,我用明确的数字做了一个错误,我把我在这里的代码:

long identifier = mMarkers.get(marker); 
long currentId = item.getId().longValue(); 

if (currentId==identifier) 
    System.out.print("They are equals"); 

在调试

enter image description here

编辑2

这是一个在调试模式下的IDE(Android Studio)的bug,我试过在另一个IDE上,它使用相同的代码很好。所以感谢大家的答案,但问题是另一个,我不能指望。

+0

如果该代码已进入if循环或无或者我们应该猜测一下你认为应该发生的事情:) – CKing

+0

@ChetanKinger代码应该在if语句中输入 –

+0

它不会编译,对于编译在数字的末尾添加L,那么这个代码应该工作原因是@ChetanKinger'if'不是一个循环,而是一个声明;) –

回答

0

首先这必须应该给你编译错误,因为编译器想起285825645621作为一个整数,但它比INT更大,所以你需要改变你的代码是这样的:

long primitiveLong = 285825645621l; //add and l at the end of your number so compiler know that it is a long not an int 
Long objectLong = new Long(285825645621l); //do the same thing here 

if(primitiveLong == objectLong.longValue()){ 
    System.out.print("They are equals"); 
} 

,这将打印:They are equals

+0

正如我所说这是我的代码更简化的版本。我没有明确的数字,但返回它们的方法 –

+0

@Fondesa,所以你不会简化你的代码!请提供更多信息以及您提供的数据,但我无法解释为什么发生这种情况。 – Lrrr

+0

查看对我的问题的编辑! –

0

您可以使用文字值还是该限制285825645

If you are using beyond the limit you will get compile error 

long primitiveLong = c; 
      Long objectLong = new Long(285825645); 

      if(primitiveLong == objectLong.longValue()){ 
       System.out.print("They are equals"); 
      }