三元运算符和意外的NullPointerException
我有时从下面的行得到NullPointerException
。
System.out.println("Date::"+ row != null ? row.getLegMaturityDate() : "null");
添加括号后,没关系。
System.out.println("Date::"+ (row != null ? row.getLegMaturityDate() : "null"));
请澄清我的行为。 提前致谢。
"Date::" + row
永远不会为null,尽管有时row
。
也就是说, "Date::"+ row != null
等同于("Date::"+ row) != null
,它始终为true。
这是运营商优先权的问题。 ChristofferHammarström有执行摘要。 有关更多详细信息,请参阅此页面http://bmanolov.free.fr/javaoperators.php 。