, : (boolean ?, type : else type ), if else. :
- if else , .
- if else , , else.
- else , , if.
- 2 3 , .
- , . *
:
System.out.println(true ? x : 0);
X, if char, else - , 0. 0 char ( 0 65535). X 0 int, int (-2 31 2 31 -1).
4 (char).
System.out.println(true ? x : i);
if - char, else - int. int, , 0 ( !).
, 2. if char else type int ( int ), else int if char, char ( i char).
int, , 2.
( , ):
System.out.println(false ? i : x);
System.out.println(false ? 0 : x);
:
final int i = 0;
System.out.println(true ? x : i);
System.out.println(false ? i : x);
, ?
* Note. All types can ultimately be converted to Objectlossless, including primitive types that can be automatically loaded (in recent versions of Java). If the method is overloaded to accept a type Object, you will never be able to experience this case, since the compiler will convert the final type to Object.
source
share