I've seen this character being used several times in tutorials and other people's projects, so I would like to know what I would use it for? It's use with 'return' specifically
Asked
Active
Viewed 116 times
-5
-
3Ternary Operator... About a billion articles online about it. The google searches *java question mark operator* or just *java question mark* all turn up relevant results. Is there a reason you asked a question? – Luke Joshua Park May 21 '16 at 04:24
-
I didn't see those questions when I typed in the title, so I didn't know those existed – diggity May 21 '16 at 05:29
1 Answers
5
I think you are asking about the ternary operator (or JLS-15.25 Conditional Operator ? :
).
return (a < b) ? a : b;
is equivalent to
if (a < b) {
return a;
} else {
return b;
}
While you asked about return
statements, it can also be used in assignments;
int t = (a < b) ? a : b;
is equivalent to
int t;
if (a < b) {
t = a;
} else {
t = b;
}
or int t = Math.min(a, b);

Elliott Frisch
- 198,278
- 20
- 158
- 249