Why does autoboxing in Java allow me to have 3 possible values for a boolean?

后端 未结 9 679
你的背包
你的背包 2020-12-06 07:43

Reference: http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html

\"If your program tries to autounbox null, it will throw a NullPointerEx

9条回答
  •  盖世英雄少女心
    2020-12-06 07:54

    There is actually no big difference to the days before Java 1.5 - the problem is not the boolean type (it still has two states) but the Boolean wrapper (which always had 3 states. Boolean.TRUE, Boolean.FALSE and null).

    Every conversion from a Boolean object to the boolean primitive requires null checks, with or without autoboxing.

提交回复
热议问题