False boolean = True?

前端 未结 7 1022
粉色の甜心
粉色の甜心 2020-12-07 02:12

I found this code in a book and I executed it in Netbeans:

boolean b = false;
if(b = true) {
    System.out.println(\"true\");
} else {
    System.out.printl         


        
7条回答
  •  [愿得一人]
    2020-12-07 02:50

    if (b = true) assigns the value true to b, and then acts on that true value. That's the danger of C-like languages, that if you leave out the second = when you meant to compare, you get an assignment instead, and an unexpected result.

提交回复
热议问题