+ 1
Please explain the meaning of this "?" in java language. Using this example int var = (7>5)?7:5;
8 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;
+ 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
+ 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
+ 1
ternary operator, which can be used as if-else.
+ 1
So ?: is an if else unary statement. I got confused because in the enhanced for loop, : means in...
+ 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).