Android的两个整数具有相同的值不正确等同 [英] Android two integers with same value not equating properly

查看:130
本文介绍了Android的两个整数具有相同的值不正确等同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是不是Java的家伙,所以我不知道,如果这仅仅是一个语言问题。

I'm in not a java guy, so I'm not sure if this is just a language issue.

我写一个Android应用程序。在应用中的一个点我比较intgers(版本号),看看他们使用的是什么版本的应用程序,如果他们不及时更新执行一些动作。我注意到,我的行动code总是被运行。所以我检查与调试,并在那里我检查,看看他们是否正在运行正确的版本,我有code是这样的:

I am writing an Android app. At one point in the app I compare to intgers (version #) to see what version of the app they are using, to perform some action if they are not up to date. I notice that my action code is always being run. So I checked with a debugger and where I check to see if they are running the correct version, I have code like this:

if (savedVersionCode != currentVersionCode){
   //perform work
}

两者 savedVersion code CURRENTVERSION code 是整数,等于相同的值(在这种情况下226),但它仍然跳跃并执行工作。

both savedVersionCode and currentVersionCode are Integers and are equal to the same value (226 in this case), but it still jumps in and performs the work.

我也注意到,尽管该值都为226,每个整数(如果你检查它在Eclipse)有一个 ID 键,它们都是不同的。

I do notice that although that values are both 226, each integer (if you inspect it in eclipse) have an id and they are both different.

这是什么怎么回事任何想法?

Any ideas on whats going on here?

推荐答案

如果他们是整数值,你比较引用。两个不同的对象可以有效地重新present相同的号码。尝试:

If they're Integer values, you're comparing the references. Two different objects can effectively represent the same number. Try:

if (savedVersionCode.intValue() != currentVersionCode.intValue())

if (!savedVersionCode.equals(currentVersionCode))

请注意,你很可能已经以较低的号码进行管理,如自动装箱会在小范围内返回0左右换号相同的参考因此,例如:

Note that you may well have managed with lower numbers, as autoboxing will return the same references for numbers in a small range around 0. So for example:

Integer x = 5;
Integer y = 5;
System.out.println(x == y); // Prints true

x = 1000;
y = 1000;
System.out.println(x == y); // *Could* print true, but probably won't

这篇关于Android的两个整数具有相同的值不正确等同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆