java三元运算符的bug?? 博客分类: Java javaJDK三元运算符
程序员文章站
2024-03-20 18:33:10
...
以前没有注意到问题,下面的TestCase运行报空指针异常。
public class A extends TestCase { public void testA() { Integer num = null; assertNull(false ? Integer.valueOf(0) : num); assertNull(false ? 0 : num); } }
class文件分析:
false ? Integer.valueOf(0) : num
public void testA(); Code: Stack=1, Locals=3, Args_size=1 0: aconst_null 1: astore_1 2: aload_1 3: astore_2 4: return
vs
false ? 0 : num
public void testA(); Code: Stack=1, Locals=3, Args_size=1 0: aconst_null 1: astore_1 2: aload_1 3: invokevirtual #15; //Method java/lang/Integer.intValue:()I 6: invokestatic #21; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 9: astore_2 10: return
总结:由于三元运算符导致,由于num为null,转为int时会出错。这算不算JDK6的bug?
推荐阅读
-
java三元运算符的bug?? 博客分类: Java javaJDK三元运算符
-
java中的复合运算符,+=,++,三元运算符,已经java中8个基本数据类型的默认值
-
原码,反码,补码的相互转换。及java中三元运算符的用法
-
Java用三元运算符判断奇数和偶数的简单实现
-
Java用三元运算符判断奇数和偶数的简单实现
-
&&运算符和||运算符的优先级问题 博客分类: Java 虚拟机JDK单元测试JVMGoogle
-
&&运算符和||运算符的优先级问题 博客分类: Java 虚拟机JDK单元测试JVMGoogle
-
JavaEE基础day02 1.定义Java中的变量 四类八种 2.变量定义和使用的注意事项 3.数据类型的转换、强制数据类型转换4.算数运算符、比较运算符、逻辑运算符、赋值运算符、三元运算符
-
Java基础学习-三元运算符和键盘录入的基本步骤和使用
-
Java菜鸟学习之路---算术运算符、赋值运算符、比较运算符、逻辑运算符、位运算符、三元运算符、运算符的优先级详解(三)