Conditional operator in concatenated string

后端 未结 4 1874
天命终不由人
天命终不由人 2020-12-11 01:35

I\'d like know why the following program throws a NPE

public static void main(String[] args) {
    Integer testInteger = null;
    String test = \"test\" + t         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-12-11 02:11

    Without the brackets it's doing this effectively: String test = ("test" + testInteger) == null ? "(null)" : testInteger.toString(); Which results in an NPE.

提交回复
热议问题