What is the efficient way to assign booleans in java?
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.
source to share
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 .
source to share