Use the Boolean.valueOf () vs method (or standalone use of Java 1.5) to create logical objects

What is the best practice between Boolean.valueOf() and Java 1.5 autoboxing to create a Boolean from booleans and why?

+6
source share
1 answer

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 .

+12
source

Source: https://habr.com/ru/post/921420/


All Articles