在 if 语句中测试 Java Long 值。 0 != 0 吗
我一生都无法弄清楚为什么我不能让这个方法进入 if 语句。
protected void foo() {
Date d = new Date();
long now = d.getTime();
long start;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
start = settings.getLong(FIRST_USE_DATE, 0);
Log.w(this.getClass().getName(), Long.toString(start));
if (start == 0) {
SharedPreferences.Editor editor = settings.edit();
editor.putLong(FIRST_USE_DATE, now);
}
返回真; 请注意,
日志和调试模式显示“start = 0”
我也尝试过
if (start == 0l) {
if (start == 0L) {
我在这里到底错过了什么? 0!=0 吗?
我正在 Eclipse 中使用 Java for Android 进行开发。谢谢。
编辑:
@methodin - 不抱歉,那不起作用。
@Aioobe - 我在 IF 语句下有一个断点,但它永远不会被创建。
Edit2:这是自从你询问以来我正在运行的实际代码。
protected boolean isDemoExpired() {
Date d = new Date();
long now = d.getTime();
long demoStart;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
demoStart = settings.getLong(FIRST_USE_DATE, 0);
if (demoStart == 0) {
SharedPreferences.Editor editor = settings.edit();
editor.putLong(FIRST_USE_DATE, now);
System.out.println(Long.toString(demoStart));
return false;
}
return true;
}
For the life of me I cannot figure out why I can’t get this method to enter the if statement.
protected void foo() {
Date d = new Date();
long now = d.getTime();
long start;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
start = settings.getLong(FIRST_USE_DATE, 0);
Log.w(this.getClass().getName(), Long.toString(start));
if (start == 0) {
SharedPreferences.Editor editor = settings.edit();
editor.putLong(FIRST_USE_DATE, now);
}
return true;
}
Note that the log and debug mode shows that “start = 0”
I also tried
if (start == 0l) {
if (start == 0L) {
What the heck am I missing here? Does 0 != 0?
I’m developing in Eclipse with Java for Android. Thanks.
Edits:
@methodin - no sorry, that does not work.
@Aioobe - I have a breakpoint under the IF statement, that never gets made.
Edit2: Here is the actual code I'm running since you've asked.
protected boolean isDemoExpired() {
Date d = new Date();
long now = d.getTime();
long demoStart;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
demoStart = settings.getLong(FIRST_USE_DATE, 0);
if (demoStart == 0) {
SharedPreferences.Editor editor = settings.edit();
editor.putLong(FIRST_USE_DATE, now);
System.out.println(Long.toString(demoStart));
return false;
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你的问题恰恰相反。
我刚刚调试了您的代码并且它可以工作,问题是 if 语句始终为 true,因为对 FIRST_USE_DATE 变量进行更改后没有 editor.commit() 。
编辑:我刚刚尝试调试您的实际代码,并且发生了同样的事情: if 语句始终为真,并且每次都会执行,因为没有 editor.commit() 来保存对 FIRST_USE_DATE 变量的更改。
I think your problem is exactly the opposite.
I've just debugged your code and it works, the problem is that the if statement it's always true because there's no editor.commit() after making the changes to the FIRST_USE_DATE variable.
Edit: I've just tried debugging your actual code and the same thing happened: the if statement it's always true and it gets made every time because there's no editor.commit() to save the changes to the FIRST_USE_DATE variable.
为什么不使用 Long start 而不是 long 并检查 null 值?
似乎这里正在发生自动拆箱,并且您获得的价值为零。另一个问题是为什么 0 == 0 检查没有通过。
Why won't you use Long start instead of long and check for null value?
it seems that auto-unboxing is happening here and you get zero value. Another question is why 0 == 0 check doesn't pass.
您在某处犯了错误...
您确定没有输入 if 语句 AND start = 0 吗?
您向我们展示您正在运行的代码吗?
您可以执行以下代码(相当于您的代码)
并且您会看到您输入了 if 语句...
You are making a mistake somewhere...
Are you sure you are not entering the if statement AND that start = 0 ?
Are you showing us the code you are running ?
You could execute following code (equivalent to your code)
And you'd see you enter the if statement...
您熟悉隐藏和阴影的概念吗?在此类或其父类之一中是否有其他使用相同名称的变量?
我不认为你的代码有什么问题 - 尝试重新启动你的计算机(它是 Microsoft 操作系统,对吗?:)
如果这不起作用,请删除有问题的代码(最终得到一个空方法)编译并测试以确保存在没有错误。然后硬编码一个可接受的值并再次测试。最后重新输入您的代码 - 不要粘贴副本。
很久以前,我有过类似的情况,直到我重新输入代码后它才消失 - 我最好的猜测是某个隐藏的字符导致了问题,但我再也没有见过它(我也不确定是什么)那一次也发生了该死的事情)。
Are you familiar with the concepts of hiding and shadowing? Do you have any other variables using the same names in this class or one of its parents?
I don't think there is anything wrong with your code - try rebooting your computer (its a Microsoft OS right? :)
If that don't work delete the offending code (end up with an empty method) compile and test to ensure there are no bugs. Then hardcode an acceptable value and test again. Finally retype your code - do not paste a copy.
Once, a very long time ago, I had something similar and it did not go away until I re-entered the code - my best guess is some hidden character was causing problems, but I have never seen it again (nor am I certain what the hell happened that time either).