+ 13
What does ? do in java
I have seen some code with a ? in a for loop and I don't know what the ? does. Can someone explain. Thanks.
9 Antworten
+ 15
?: is ternary oprator
If you have some condition and you want find what is true or false
Than use it ternary oprator
For Example :
a=10 ,b=5;
If you use if else statment
if(a > b)
Print(true);
else
Print(false);
And if you use ternary oprator
Print(a>b?true:false);
+ 11
The conditional operator, ? : , is a shorthand for an if-then-else construction that does one of two different things depending upon the result of a boolean expression.
The expression:
variable = booleanExpr ? value1 : value2;
has the same effect as:
if (booleanExpr) {
variable = value1;
} else {
variable = value2;
}
• The conditional operator is also sometimes called the 'ternary operator' or the 'question-colon operator'.
For example, in the expression:
isHungry() ? eat() : beMerry();
isHungry() will be evaluated first. If it evaluates to 'true' , eat() will be evaluated and beMerry() will not. Otherwise, beMerry() will be evaluated and eat() will not.
+ 9
J 12323123
( ? ) Check the options where result equals "foo"
□ result = 2 > 4 ? "foo" : "bar";
□ result = 2 < 4 ? "foo" : "bar";
□ result = 2 > 4 ? "bar" : "foo";
► NOTE: that Java, in a manner similar to C#, only evaluates the used expression and will not evaluate the unused expression.
+ 8
? : is the ternary operator
a = b ? c : d
is equivalent to
if(b)
a = c;
else
a = d;
x ? y : z ==> if x is true, do y, else z
+ 8
➝ Remember to use 🔍Search... bar to avoid from posting duplicate threads!
https://www.sololearn.com/Discuss/610534/?ref=app
https://www.sololearn.com/Discuss/787502/?ref=app
https://www.sololearn.com/Discuss/535721/?ref=app
+ 7
System.out.println(true?true:false);
//is equivalent to
if(true){System.out.println(true);}
else{System.out.println(false);}
+ 5
Speedy response!
+ 3
int max(int x, int y) {
return x>y ? x : y;
}
+ 2
wao great reply