可以,然后呢..
// As you already stated, you know that a boolean defaults to false.
boolean turnedOn;
if(turnedOn) // Here, you are saying "if turnedOn (is true, that's implicit)
{
//then do this
}
else // if it is NOT true (it is false)
{
//do this
}
现在更有意义吗?
if语句将评估您放入其中的任何返回布尔值的代码,如果评估返回true,则输入第一个块.否则(如果值不为真,它将为false,因为布尔值可以为true或false)它将进入 – 是的,你猜对了 – else {}块.
一个更冗长的例子.
如果我被问到“你饿了吗?”,简单的回答是肯定的(真实的).或者不(假).
boolean isHungry = true; // I am always hungry dammit.
if(isHungry) { // Yes, I am hungry.
// Well, you should go grab a bite to eat then!
} else { // No, not really.
// Ah, good for you. More food for me!
// As if this would ever happen - bad example, sorry. ;)
}