int m1 = a < b ? a : b ...what is the meaning of the question mark between a < b ? a : b
what is the meaning of the question mark between a < b ? a : b
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
codeNoob
31 December 2020, 09:10
(condition) ? (if true, do this) : (otherwise, do this)
This is a ternary operator which I would recommend you google search to further deepen your knowledge
0
Nick
2 December 2020, 15:57
As zimme mentioned, this is what is called a ternary operator (ter meaning 3). Essentially, this is an operation that operates on three different elements (called operands). The basic setup is as follows:
<conditional that will be evaluated> ? <returned if conditional is true> : <returned if conditional is false>
so for above, we have the following
a < b ? a : b
a < b ----> this is the conditional that is being evaluated. It is either a true or false statement
If a < b is true, it will return the value to the left of the colon (:), which is a
If a < b is false, it will return the value to the right of the colon (:). which is b
Essentially, ternary operations are just a more streamlined approach towards writing if/else statements. We can do the same thing in a more long-handed fashion with an if/else statement:
if (a < b)
return a;
else
return b;
0
zimme
2 December 2020, 06:29
read about ternary operator in java
0
hidden #10687429
2 December 2020, 03:49
there doesnt need to be a ? you might want to reseting your code if you cant figure out what it was
0