public class Test {
static boolean b = true;
public static void main(String[] args){
b = false;
if(b = false){
System.out.println("false");
}
else{
System.out.println("true");
}
}
}
为什么运行结果是true?
static boolean b = true;
public static void main(String[] args){
b = false;
if(b = false){
System.out.println("false");
}
else{
System.out.println("true");
}
}
}
为什么运行结果是true?
应该写成这样:
if (b) {
...
}
而不要写成这样
if (b == true) {
...
}
后者不仅效率不高,而且可能写错。楼主把双等号写成单等号,变成了赋值语句。