Why String concatenate null with + operator and throws NullPointerException with concate() method

后端 未结 9 1771
别那么骄傲
别那么骄傲 2021-02-04 06:23

Here is my class, where i am concatenating two string. String concatenate with null using + operator execute smoothly but throws NullPointerException

9条回答
  •  野性不改
    2021-02-04 06:49

    String.concat() need an object of type String as a parameter.

    There is no type which null is an instanceof. Refer JLS:

    15.20.2 Type Comparison Operator instanceof

    RelationalExpression: RelationalExpression instanceof ReferenceType At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

提交回复
热议问题