+ 4
What is conditional operator in java and how can i use it?
8 Answers
+ 9
(expression) ? evaluates if true: evaluates if false
+ 9
For example:
System.out.print((1>0)?true:false);
// Outputs true
+ 7
Conditional operators help produce a condition uaing compasrison
+ 7
@Roman Correct.
+ 3
@ Roman those are relational operators
I ment these ( ? : )
+ 3
result = testCondition ? value1 : value2
+ 2
== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
+ 2
if (x>y){
x+=y
}else{
x*=y
}
with ternary operator would be (x>y)?x+=y:x*=y;
The ternary operator is a conditional operator that takes 3 operands, its has a more compact syntax that if-else statements, reason to use ternary condition is to make code shorter and more readable