SXV
SXV

Reputation: 277

Which statement (true or Boolean.TRUE) will be more efficient

My bean is as below

private boolean myBoolean;

public boolean isMyBoolean() {
    return myBoolean;
}

public void setMyBoolean(
        boolean myBoolean) {
    this.myBoolean = myBoolean;
}

Now when I use setter for the above Boolean field then what should be the efficient way to do it

setMyBoolean(true);

or

setMyBoolean(Boolean.TRUE);

I know that autoboxing will take care and both will work But I don't know what is the efficient way in this example. So my question is which of both should I use to write an efficient code OR both are equally good

TIA

Upvotes: 2

Views: 498

Answers (5)

kotacc
kotacc

Reputation: 327

Boolean.TRUE is a wrapper object for boolean.

Boolean class implementation from Sun SDK:

public final class Boolean ... 
{
    private final boolean value;

    public static final Boolean TRUE = new Boolean(true);
    public static final Boolean FALSE = new Boolean(false);
    ...
}

when you use setMyBoolean(Boolean.TRUE);:

  1. VM creates Boolean object (in heap) and the saves the value to true.
  2. Unboxes the created object and finally assigns primitive true to myboolean.

when you use setMyBoolean(true);: only step 2 will be performed from above. Thus faster!

Upvotes: 0

bubooal
bubooal

Reputation: 621

It does not matter. At the very end when your code is compiled the bytecode generated will end up using the boolean data type on the JVM.

What I am trying to say is, on compile time, such a simple code like this will be optimized automatically for the JVM. It does not matter which way you use, remember that the compiler will end up translating this into bytecode and JVMs nowadays do this kind of optimizations all the time.

If you are skeptical on this, you can try both ways and use a profiler on your code. You will be amazed.

Upvotes: 0

No Idea For Name
No Idea For Name

Reputation: 11577

The reason

boolean boolVar = Boolean.TRUE;

works is because of autounboxing, a Java 5 feature that allows a wrapper object to be converted to its primitive equivalent automatically when needed

as you can understand, setting is faster then autounboxing and then setting...

Upvotes: 1

MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29166

Use the first one. More readable. You don't need to concern about the performance issue here, although the second one will involve auto-boxing and comparatively slower (again, you don't need to think about it).

Only consider code readability in this type of cases, and remember that primitives will always be faster than objects.

Upvotes: 4

Suresh Atta
Suresh Atta

Reputation: 121998

imho

 setMyBoolean(true);

Primitives always do favor than Wrappers.Where ever I am able to use primitives,I go for them

Because while run time, If we use Wrappers Boxing conversions and Unboxing Conversions happens at Runtime,Obviously that takes more time.

In your case

At run time, boxing conversion proceeds as follows:

If p is a value of type boolean, then boxing conversion converts p into a reference r of class and type Boolean, such that r.booleanValue() == p

If you use primitive there,Obviously you save that time.

Upvotes: 1

Related Questions