Null values of Strings and Integers in Java

Your code makes use of two different additive operators. The first three lines use string concatenation, whereas the last one uses numeric addition.

String concatenation is well-defined to turn null into "null":

  • If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l).

Hence there is no NPE.

Adding two Integer objects together requires them to be unboxed. This results in the null reference being dereferenced, which leads to the NPE:

  • If r is null, unboxing conversion throws a NullPointerException

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)