Netbeans Java Debugger claims that ((true && false) == true)

后端 未结 2 1905
忘了有多久
忘了有多久 2021-02-13 13:00

I just encountered something that defies my understanding of logic. How can the situation below occur?

\"enter

2条回答
  •  梦谈多话
    2021-02-13 13:13

    I see a couple of possibilities, but I don't believe it is internally wrong in the JVM. The debugger is probably simply tricked or bugged.

    1. Some optimization is going on under the hood that causes left and complete be the same variable on the stack. So this would roughly means that your code got optimized to this:

      boolean left = (start <= offset);
      boolean right = (stop + 1 >= offset);
      left = left && right;   // reused "left" instead of new variable "complete"
      

      However, as far as I know, Java compilers don't do this sort of optimization. Can someone confirm or give details if this is not true? (Maybe javac or the JIT does this?)

    2. NetBeans debugger is really bugging. From my C++ debugging experience, there actually existed a bug in a debugger (sounds funny, right) that causes the debugger to be unable to read integer values from memory correctly. Sometimes the results were off. This doesn't mean anything in this case, but it actually is possible that debuggers do have bugs.

      I remember I've been searching for hours to fix a bug in my code I discovered by debugging. But there was no bug. At least not in my code. The debugger reported me some values in memory, but were wrong.

    If this weird behavior happens always, then try to put a debug statement behind it:

    System.out.println(left + " && " +  right + " == " + complete);
    

    I bet the output will be correct. Try to run the debugger also with this line added. If such an optimization happens as I described, it should be gone, because it can't reuse left anymore.

提交回复
热议问题