What is the difference between while (x = false) and while (!x) in Java?

陌路散爱 提交于 2019-12-01 21:18:10
Joren

Note the difference between done = false and done == false. The first one assigns done to be false and evaluates as false, the second one compares done with false and is exactly identical to !done.

So if you use:

while (done = false)
{
 // code here
}

Then done is set to false and the code within the while loop doesn't run at all.

The statement x = false is an assignment - you are setting x to false. The statements x == false and !x are the same, however. The statement x == false compares x to false and will be true if the value of x is false and false if the value of x is true. The statement !x will result in the negation of the value of x.

In your code, if you replace while (!inputDone) with while(inputDone == false), you will get the expected behavior.

You need to use == instead of = for comparisons.

"while(done = false)" is equals to "done=false; while(done)"

It should be written as "while(done == false)" or "while(false == done)".

But still , !done is the most readable code, it say "NOT DONE"

As many others have pointed out, you have typoed ==. More interesting are the issues surrounding this.

For language designed: Encouraging side-effects in expressions is bad. Using the symbol == to represent mathematical = is not a good choice.

In terms of readability, !done reads much better than done == false - we want "not done" (better, IMO, would be "until done" instead of "while not done"). Although (perpetual) newbies often write the redundant someCondition == true.

It is a good idea to make variables final, although clearly not feasible in this situation. However, we can remove the flag entirely by using a break statement. A minority opinions follows a Single Entry Single Exit (SESE) rule, whereby break is banned, whcih would make this example more tricky (you'd need a test to see if the text was a valid int, or in this case, move the body into the loop.

Mike

The expression:

x = false

means assign x the value false.

After this happens in your while(), it then evaluates x, which is false so it doesn't enter the loop at all.

On the other hand:

while (!x)

means "as long as !x is true, continue entering the loop". since !x means "the opposite of x". So as long as x is false, the loop will continue

Stephen C

Other answers have alluded to the fact that writing x == false and x == true are bad style. There are three reasons for this:

  • Conciseness: assuming that you are in a context where a Boolean is required, and "x" is a Boolean, it is less characters to write x than x == true, or !x than x == false.
  • Convention: seasoned programmers in Java (or C, C++, C# and most other languages) expect to see x rather than x == true, and !x rather than x == false.
  • Robustness: in Java the conditional and loop statements all require a Boolean valued expression in the condition. If y is not a Boolean, then a typo of the form if (y = foo) { will give a compilation error in Java. But if y is a Boolean then if (y = foo) { does not give a compilation error. Hence, by avoiding == for Booleans you avoid setting yourself up for a whole raft of bugs resulting from typos.
Jeff Olson

Many have already pointed out your misuse of = vs. ==. I would like to point out that running a static code analysis tool like Findbugs would have found this for you right away.

See QBA: Method assigns boolean literal in boolean expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!