nam*_*olk 16 java int autoboxing
可能重复:
Java中的棘手三元运算符 - 自动装箱
我们知道int roomCode = null;编译器不允许这样做.
那么为什么代码1在代码2的情况下不会给出编译器错误.
代码1:
int roomCode = (childCount == 0) ? 100 : null;
Run Code Online (Sandbox Code Playgroud)
代码2:
int roomCode = 0;
if(childCount == 0) roomCode = 100;
else roomCode = null; // Type mismatch: cannot convert from null to int
Run Code Online (Sandbox Code Playgroud)
tal*_*las 11
我做了一些调试,发现在评估时
(childCount == 0) ? 100 : null;
Run Code Online (Sandbox Code Playgroud)
程序调用valueOfInteger 的方法来评估null.它返回一个Integer,并且作为一个Integer可以为null(而不是int),它会编译.就像你做的那样:
int roomCode = new Integer(null);
Run Code Online (Sandbox Code Playgroud)
所以它与自动装箱有关.
| 归档时间: |
|
| 查看次数: |
1369 次 |
| 最近记录: |