在 if 语句中测试 Java Long 值。 0 != 0 吗

发布于 2024-09-30 10:32:40 字数 1294 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的鱼塘能养鲲 2024-10-07 10:32:40

我认为你的问题恰恰相反。
我刚刚调试了您的代码并且它可以工作,问题是 if 语句始终为 true,因为对 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();***       
    }
}

编辑:我刚刚尝试调试您的实际代码,并且发生了同样的事情: 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;
}

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.

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.

    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;
}
沒落の蓅哖 2024-10-07 10:32:40

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

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

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

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

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.

后来的我们 2024-10-07 10:32:40

您在某处犯了错误...

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

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

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

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

并且您会看到您输入了 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)

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

巾帼英雄 2024-10-07 10:32:40

您熟悉隐藏和阴影的概念吗?在此类或其父类之一中是否有其他使用相同名称的变量?

我不认为你的代码有什么问题 - 尝试重新启动你的计算机(它是 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文