Please explain the meaning of this "?" in java language. Using this example int var = (7>5)?7:5; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please explain the meaning of this "?" in java language. Using this example int var = (7>5)?7:5;

26th May 2019, 1:50 PM
Iredafe Owolabi
Iredafe Owolabi - avatar
7 Answers
+ 4
The ? is an operator. (Syntax: x? y: z) It tests the condition before the question mark (x), and returns the first result (y) if the condition is true, else the second one (z). It is basically a shortcut for this: if (7 > 5) var = 7; else var = 5;
26th May 2019, 1:55 PM
Airree
Airree - avatar
+ 4
Bro you haven't named your variable Well ? is used to make choices between the two. As in ur example (7>5) = true As it is true , it will take the value of var as 7 which is the 1st element. But if the condition results to false the value of var will be assigned as the 2nd element Thanks
26th May 2019, 1:56 PM
Prince PS[Not_Active]
Prince PS[Not_Active] - avatar
+ 3
That statement like "?" called Ternary, which mean variable have an value if IF conditional with statement true or false. Int var (7>5) ? 7 : 5 7 > 5 . That statement is true and you got value 7. That's all i know. Thanks
26th May 2019, 2:12 PM
Rizki Rivaldi
+ 1
ternary operator, which can be used as if-else.
26th May 2019, 1:58 PM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar
+ 1
So ?: is an if else unary statement. I got confused because in the enhanced for loop, : means in...
26th May 2019, 2:00 PM
Iredafe Owolabi
Iredafe Owolabi - avatar
26th May 2019, 2:07 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
? - Its a ternary operator. It is similar to if and else conditions. Var = (7>5)?7:5 First it checks whether 7 is greater than 5, if its true 7 is stored into variable "var". If its not greater than 5 then var=5. var = condition?a:b var = a (if condition is satisfied) var = b (if condition is not satisfied).
26th May 2019, 4:22 PM
Veda Shree S
Veda Shree S - avatar