Autoboxing boolean
transparently translated into Boolean.valueOf()
compiler:
boolean b = true; Boolean bb = b;
translates to:
iconst_1 istore_1 //b = 1 (true) iload_1 //b invokestatic #2; //Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean; astore_2 //bb = Boolean.valueOf(b)
Use what you find more useful and readable. Since using Boolean.valueOf()
gives you nothing but extra text input, you should aim for autoboxing.
The situation complicates when you think about the opposite transformation - from boolean
to boolean
. This time, Boolean.booleanValue()
is called transparently by the compiler for you, which could theoretically NullPointerException
.
source share