I discovered this surprising thing while learning OCA/OCP for Java.
Below is the first piece of code of which the if(test condition) part
surprises me.
public class BooleanIf {
public static void main(String[] args) {
boolean b = false;
System.out.println(Boolean.valueOf(b = true));
if (b = true)
System.out.println("true");
else
System.out.println("false");
}
Now the output of this surprisingly is "true".
I learnt that there has to be a relational condition that returns true or false like if (a > b)
or if (a != b)
likewise.
I want to know how it is returning true for this case. Does it call Boolean.valueOf()?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…