Enlightenment on Boolean==Boolean and Boolean=Boolean

I did a java interview question last night, and the original question is like this (from the most basic interview questions in JAVA_ MOOC Notes http://www.imooc.com/article/13754 )

6.  给出以下代码,请问该程序的运行结果是什么?
class Example{
public static void main(String args[]){
boolean flag=false;
if(flag=true){
System.out.println(“true”);
}
else{
System.out.println(“false”);
}
}
}
A   代码编译失败,if语句错误。
B   打印输出true。
C   打印输出false。
D   无内容输出。

At that time, I thought that the if statement should not be assigned a value, so I chose A decisively. .

However, the correct result is B, that is, true is output, because although the flag variable is initialized to false when declaring the variable here, it is changed to the literal value true by using the "=" assignment symbol when judging if . . if(true){}

Another: If the if judgment uses "==" logic equality, it is obvious that the comparison result is false, and "false" is output.

Mention ==, there is also an equals, you can refer to the following link for understanding~



Question about if(Boolean=Boolean). . -CSDN Forum 

[Java] The difference between double equal signs and equals - If youth is a naive stubbornness, you are the persistence I care most about. - CSDN blog

Guess you like

Origin blog.csdn.net/qq_28421553/article/details/75219351