1

To put it in code - which has better performance (if there is a difference at all)?

Given this:

public class Customer
{
    ....

    public Boolean isVIP(){...}
    ...
}

Which is faster?

public void handleCustomer(Customer customer)
{
    if (customer.isVIP())  // Auto Unboxing
    {
        handleNow(customer);
    }
    else
    {  
        sayHandlingNowButQueueForTomorrow(customer);
    }
}

or this:

public void handleCustomer(Customer customer)
{
    if (customer.isVIP().booleanValue()) // Explicit unboxing
    {
        handleNow(customer);
    }
    else
    {  
        sayHandlingNowButQueueForTomorrow(customer);
    }
}
RonK
  • 9,472
  • 8
  • 51
  • 87
  • 1
    I think you have the meaning of "implicit" reversed - if you call the `.booleanValue()` method to unbox the value yourself, that is *explicit* – matt b Aug 02 '11 at 19:02
  • @matt - 10x, i updated the question – RonK Aug 02 '11 at 19:56

4 Answers4

14

No difference between them, you can verify it in the bytecode:

public class ImplicitTest {
    public static void main(String[] args) {
        Boolean b = true; 
        boolean i = b;
        boolean e = b.booleanValue();
    }
}

Run javap to see what it compiles to:

javap -c ImplicitTest

Here is the output:

Compiled from "ImplicitTest.java"
public class ImplicitTest extends java.lang.Object{
public ImplicitTest();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   iconst_1
   1:   invokestatic    #2; //Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
   4:   astore_1
   5:   aload_1
   6:   invokevirtual   #3; //Method java/lang/Boolean.booleanValue:()Z
   9:   istore_2
   10:  aload_1
   11:  invokevirtual   #3; //Method java/lang/Boolean.booleanValue:()Z
   14:  istore_3
   15:  return

}

As you can see - lines 5,6,9 (implicit) are the same as 10, 11, 14 (explicit).

Per Lundberg
  • 3,837
  • 1
  • 36
  • 46
Mark D
  • 5,368
  • 3
  • 25
  • 32
8

The difference should all be at compile time, since auto unboxing is just syntactic sugar. In this case the Java bytecode generated should be exactly the same. This means no difference at runtime. However, in the more general case explicit unboxing may be faster, because implicit unboxing may unbox the value more than once, while with explicit unboxing you can guarantee that the value is unboxed only once and the result stored.

dsimcha
  • 67,514
  • 53
  • 213
  • 334
  • You are right about the potential of auto unboxing to repeat itself - luckily, that is not my case. – RonK Aug 03 '11 at 04:29
  • 1
    This answer is in a way better than the accepted one, because it states principles that help us generalize beyond one example to how unboxing can perform in general. – LarsH Aug 23 '17 at 16:05
3

Performance wise, they ideally should be the same.

There's a chance that the human written techniques are a bit less optimal, so it might be a performance hit if you do poor human written methods of autoboxing. But, if you really want to get to it, there's an equal chance that a human might write some sort of non-general solution which beats default performance. Such a solution would not be as flexible, and it would probably trade off computational complexity for memory (like a big lookup array).

Personally, I'd recommend taking some time to really view the larger picture. Optimizing a single line or two of code is almost never a good investment. Reducing the amount of work necessary in the entire program is more likely to get you your performance boosts.

Note that in the general case, the JVM didn't change with the introduction of autoboxing, just the compiler did. So the compiler is adding the same instructions as you would write out manually in the most common cases. The performance is measure in the JVM at runtime, and if it's the same bytecodes either way, there's no reason to expect a performance difference.

This just smacks of premature optimization, but if you think you can find a difference in time: do so with careful testing, and then realize that it may be different on different point releases, operating systems, etc. It's just not a clear cut win in any case.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • I'm not asking this since I think this single line will solve a performance problem, I just saw that our code base is filled with '.booleanValue()' which is a result of code written for Java 1.4. I wanted to be sure that by switching to implicit unboxing I will not cause performance degradation throughout the application as we have hundreds of such lines – RonK Aug 03 '11 at 04:28
  • I would recommend against relying on autoboxing if they already wrote the code to do it manually. Basically the compiler just adds in the ".booleanValue()" invisibly. I'm not a fan of "invisible" code. In this case, it's sort of how the compiler automatically adds in the construction of a StringBuilder and does a ton of appends to handle the "String" + "String" + "String" routines. Perhaps it's a bit overzealous, but I tend to add explicit calls to `super()` too in my constructor. In other words, it's either six to one person, or half-a-dozen to another. – Edwin Buck Aug 05 '11 at 14:56
0

Is a VIP so VI that it must return Boolean instead of boolean?

irreputable
  • 44,725
  • 9
  • 65
  • 93