Test.java content:
public class Test {
public Boolean mBoolean;
public void foo() {
this.mBoolean = Boolean.TRUE;
}
}
Test2.java content:
public class Test2 {
public Boolean mBoolean;
public void foo() {
this.mBoolean = true;
}
}
# javap -c Test.class
...
public void foo();
Code:
0: aload_0
1: getstatic #2 // Field java/lang/Boolean.TRUE:Ljava/lang/Boolean;
4: putfield #3 // Field mBoolean:Ljava/lang/Boolean;
7: return
# javap -c Test2.class
...
public void foo();
Code:
0: aload_0
1: iconst_1
2: invokestatic #7 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
5: putfield #13 // Field mBoolean:Ljava/lang/Boolean;
8: return
So apparently it actually calls Boolean.valueOf(true)
when you assign the primitive value which results in a few more instructions.
Keep in mind though that Boolean.valueOf
returns either Boolean.TRUE
or Boolean.FALSE
no instance creation is done.
The performance impact (in my opinion) is thus neglible and it can be argued for either case and may be a matter of style.
The JIT might optimize the call away anyways.