Java中的if(布尔条件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19152656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
if (boolean condition) in Java
提问by rickygrimes
This is always very confusing to me. Can someone please explain it? The confusion I have is - boolean default to false. So in the below example, does it enter the if
loop when state is not turned on, i.e., it is TURNED OFF if (condition is false)
ORdoes it enter the if
loop when state is TURNED ON, in other words if (condition is true)
?
这对我来说总是很困惑。有人可以解释一下吗?我的困惑是 - 布尔值默认为 false。因此,在下面的例子中,它进入if
循环状态时,没有打开,即,它被关闭if (condition is false)
或它进入if
时的状态被打开,换句话说循环if (condition is true)
?
boolean turnedOn;
if (turnedOn) {
//do stuff when the condition is false or true?
} else {
//do else of if
}
I know this is a very basic question, but if you could explain the answer in very basic language, that would be great. :) Feel free to point me to duplicate posts that have a very good explanation (I did not find one where I could clearly get it). Also feel free to change the subject of the post if you'd like to make it more generic.
我知道这是一个非常基本的问题,但是如果您能用非常基本的语言解释答案,那就太好了。:) 随意指出我重复的帖子,这些帖子有很好的解释(我没有找到一个我可以清楚地得到它的地方)。如果您想让帖子更通用,也可以随意更改帖子的主题。
采纳答案by Jeff
Okay, so..
可以,然后呢..
// 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
}
Does it make more sense now?
现在更有意义了吗?
The if
statement will evaluate whatever code you put in it that returns a boolean value, and if the evaluation returns true, you enter the first block. Else(if the value is nottrue, it will be false, because a boolean can either be true or false) it will enter the - yep, you guessed it - the else {}
block.
该if
语句将评估您放入其中的返回布尔值的任何代码,如果评估返回 true,则您进入第一个块。否则(如果值不为真,它将为假,因为布尔值可以为真或假)它将进入 - 是的,你猜对了 -else {}
块。
A more verbose example.
一个更详细的例子。
If I am asked "are you hungry?", the simple answer is yes (true). or no (false).
如果我被问到“你饿了吗?”,简单的答案是肯定的(真的)。或否(假)。
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. ;)
}
回答by logoff
boolean state = "TURNED ON";
is not a Java valid code. boolean can receive only boolean values (true or false) and "TURNED ON"
is a String.
不是 Java 有效代码。boolean 只能接收布尔值(真或假)并且"TURNED ON"
是一个字符串。
EDIT:
编辑:
now you are talking about a loop and your code does not contain any. your var state
is false because the boolean default value and you execute the else clause.
现在您正在谈论一个循环,而您的代码不包含任何内容。你的 varstate
是假的,因为布尔默认值,你执行 else 子句。
回答by sp00m
Booleans default value is false only for classes' fields. If within a method, you have to initialize your variable by true or false. Thus for example in your case, you'll have a compilation error.
Booleans 默认值仅对于类的字段为 false。如果在一个方法中,您必须通过 true 或 false 来初始化您的变量。因此,例如在您的情况下,您将遇到编译错误。
Moreover, I don't really get the point, but the only way to enter within a if is to evaluate the condition to true.
此外,我真的不明白这一点,但进入 if 的唯一方法是将条件评估为真。
回答by Juned Ahsan
Assuming state
is having a valid boolean value set in your actual code, then the following condition will succeed
假设state
在您的实际代码中设置了一个有效的布尔值,那么以下条件将成功
if(state)
when state is boolean value is TRUE
当状态为布尔值时为 TRUE
If condition checks for the expression whether it is evaluated to TRUE/FALSE. If the expression is simple true
then the condition will succeed.
If 条件检查表达式是否被评估为 TRUE/FALSE。如果表达式很简单,true
则条件将成功。
回答by SudoRahul
This is how the if
behaves.
这就是if
行为的方式。
if(turnedOn) // This turnedOn should be a boolean or you could have a condition here which would give a boolean result.
{
// It will come here if turnedOn is true (i.e) the condition in the "if" evaluates to true
}
else
{
// It will come here if turnedOn is false (i.e) the condition in the "if" evaluates to false
}
回答by Simon Dorociak
if (turnedOn) {
//do stuff when the condition is false or true?
}
else {
//do else of if
}
It can be written like:
它可以写成:
if (turnedOn == true) {
//do stuff when the condition is false or true?
}
else { // turnedOn == false or !turnedOn
//do else of if
}
So if your turnedOn
variable is true, ifwill be called, if is assigned to false, elsewill be called. boolean values are implicitly assigned to false if you won't assign them explicitly e.q. turnedOn = true
因此,如果您的turnedOn
变量为真,则将调用 if ,如果将其分配给 false,则将调用else。如果您不显式分配布尔值,则将它们隐式分配给 false eqturnedOn = true
回答by TheLostMind
boolean turnedOn;
if(turnedOn)
{
//do stuff when the condition is true - i.e, turnedOn is true
}
else
{
//do stuff when the condition is false - i.e, turnedOn is false
}
回答by Safinn
In your example, the IF statement will run when it is state = true meaning the else part will run when state = false.
在您的示例中,IF 语句将在 state = true 时运行,这意味着 else 部分将在 state = false 时运行。
if(turnedOn == true) is the same as if(turnedOn)
if(turnedOn == false) is the same as if(!turnedOn)
If you have:
如果你有:
boolean turnedOn = false;
Or
或者
boolean turnedOn;
Then
然后
if(turnedOn)
{
}
else
{
// This would run!
}
回答by Subir Kumar Sao
The syntax of if block is as below,
if 块的语法如下,
if(condition){
// Executes when condition evaluates to true.
}
else{
// Executes when condition evaluates to false.
}
In your case you are directly passing a boolean value so no evaluation is required.
在您的情况下,您直接传递一个布尔值,因此不需要评估。
回答by Albert Laure
ABoolean (with a uppercase 'B') is a Boolean object, which if not assigned a value, will default to null. boolean (with a lowercase 'b') is a boolean primitive, which if not assigned a value, will default to false.
ABoolean(带有大写的“B”)是一个布尔对象,如果未赋值,则默认为 null。boolean(带有小写的 'b')是一个布尔基元,如果没有赋值,默认为 false。
Boolean objectBoolean;
boolean primitiveBoolean;
System.out.println(objectBoolean); // will print 'null'
System.out.println(primitiveBoolean); // will print 'false'
so in your code because boolean with small 'b' is declared it will set to false hence
所以在您的代码中,因为声明了带有小“b”的布尔值,因此它将设置为 false
boolean turnedOn;
if(turnedOn) **meaning true**
{
//do stuff when the condition is false or true?
}
else
{
//do else of if ** itwill do this part bechae it is false
}
the if(turnedon) tests a value if true, you didnt assign a value for turned on making it false, making it do the else statement :)
if(turnedon) 测试一个值如果为真,你没有分配一个值来使它成为假,让它做 else 语句:)