What is the efficient way to assign booleans in java?
We can assign a value in boolean
three ways
boolean isDone;
isDone = true;
isDone = Boolean.valueOf(true);
isDone = Boolean.TRUE;
so which appointment is more efficient?
Only the first is not related to boxing or unboxing. Thus, at first glance, the former will be the most effective. However, most compilers (or just-in-time compilers, if any) are likely to optimize the other two assignments just as effectively.
The story would be different, of course, if isDone
declared as Boolean
instead Boolean
. In this case, the third assignment would be my preference.
Chances are the jit compiler and optimizer will optimize all of them anyway, but the "best" is theoretically the first: isDone = true;
since it doesn't include boxing and unboxing .