eclipse 在 if 语句中测试 Java Long 值。是否 0 != 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4078009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:16:41  来源:igfitidea点击:

Testing a Java Long value in an if statement. Does 0 != 0

javaandroideclipselong-integer

提问by user432209

For the life of me I cannot figure out why I can't get this method to enter the if statement.

对于我的生活,我无法弄清楚为什么我无法使用此方法输入 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);        
    }

return true; }

返回真;}

Note that the log and debug mode shows that “start = 0”

注意日志和调试模式显示“start = 0”

I also tried

我也试过

if (start == 0l) {
if (start == 0L) {

What the heck am I missing here? Does 0 != 0?

我到底错过了什么?0 != 0 吗?

I'm developing in Eclipse with Java for Android. Thanks.

我正在 Eclipse 中使用 Java for Android 进行开发。谢谢。

Edits:

编辑:

@methodin - no sorry, that does not work.

@methodin - 不抱歉,这不起作用。

@Aioobe - I have a breakpoint under the IF statement, that never gets made.

@Aioobe - 我在 IF 语句下有一个断点,它永远不会被创建。

Edit2: Here is the actual code I'm running since you've asked.

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;
    }

回答by Telmo Marques

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.

我认为你的问题恰恰相反。我刚刚调试了您的代码并且它可以工作,问题在于 if 语句始终为真,因为在对 FIRST_USE_DATE 变量进行更改后没有 editor.commit() 。

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);
        ***editor.commit();***       
    }
}

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.

编辑:我刚刚尝试调试您的实际代码并且发生了同样的事情:if 语句始终为真,并且每次都会执行,因为没有 editor.commit() 来保存对 FIRST_USE_DATE 变量的更改。

    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);
        ****editor.commit();****
        System.out.println(Long.toString(demoStart)); 
        return false;

    }
    return true;
}

回答by BigMac66

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? :)

我认为您的代码没有任何问题 - 尝试重新启动计算机(它是 Microsoft 操作系统,对吗?:)

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).

很久以前,我有过类似的东西,直到我重新输入代码它才消失 - 我最好的猜测是某些隐藏的字符导致了问题,但我再也没有见过它(我也不确定是什么那个时候也发生了地狱)。

回答by Vladimir Ivanov

Why won't you use Long start instead of long and check for null value?

为什么不使用 Long start 而不是 long 并检查空值?

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 == null) {
        SharedPreferences.Editor editor = settings.edit();
        editor.putLong(FIRST_USE_DATE, now);        
    }
}

it seems that auto-unboxing is happening here and you get zero value. Another question is why 0 == 0 check doesn't pass.

似乎在这里发生了自动拆箱,而您的价值为零。另一个问题是为什么 0 == 0 检查没有通过。

回答by pgras

You are making a mistake somewhere...

你在某个地方犯了一个错误......

Are you sure you are not entering the if statement AND that start = 0 ?

您确定没有输入 if 语句 AND that start = 0 吗?

Are you showing us the code you are running ?

你在向我们展示你正在运行的代码吗?

You could execute following code (equivalent to your code)

您可以执行以下代码(相当于您的代码)

public static void main(String[] args) {
    long start = 0;
    if (start == 0) {
        System.out.println(Long.toString(start));
    }
}

And you'd see you enter the if statement...

你会看到你输入了 if 语句......