+ 4

What is conditional operator in java and how can i use it?

11th Nov 2017, 12:11 PM
Abraham Yonas
Abraham Yonas - avatar
8 odpowiedzi
+ 9
(expression) ? evaluates if true: evaluates if false
11th Nov 2017, 12:48 PM
qwerty
qwerty - avatar
+ 9
For example: System.out.print((1>0)?true:false); // Outputs true
11th Nov 2017, 12:50 PM
qwerty
qwerty - avatar
+ 7
Conditional operators help produce a condition uaing compasrison
11th Nov 2017, 12:52 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 7
@Roman Correct.
11th Nov 2017, 12:55 PM
qwerty
qwerty - avatar
+ 3
@ Roman those are relational operators I ment these ( ? : )
11th Nov 2017, 12:39 PM
Abraham Yonas
Abraham Yonas - avatar
+ 3
result = testCondition ? value1 : value2
11th Nov 2017, 12:49 PM
Roman
Roman - avatar
+ 2
== equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to
11th Nov 2017, 12:20 PM
Roman
Roman - avatar
+ 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
11th Nov 2017, 1:00 PM
Roman
Roman - avatar