1

This is an exact statement from a book on Java. Can someone confirm whether this is indeed right ?

And why does the operation cost done on wrapper classes will be higher than their primitive counterpart ?

UnderDog
  • 3,173
  • 10
  • 32
  • 49
  • possible duplicate of [Does boxing cause performance issues?](http://stackoverflow.com/questions/8468266/does-boxing-cause-performance-issues) – assylias Aug 31 '13 at 06:17
  • And many other: http://stackoverflow.com/search?q=%5Bjava%5D+boxing+performance&tab=relevance – assylias Aug 31 '13 at 06:19
  • If the performance and memory usage was the same you wouldn't need primitives. The compiler can optimise usage but not completely. – Peter Lawrey Aug 31 '13 at 06:30

3 Answers3

3

Of course this is right. A lot of things come into picture when you use wrapper classes:

1) Implicit conversion of primitive to Objects, this is not easy a lot of private variables of the wrapper class are populated to create an object for a primitive. Which I believe is an overhead.

2) They also have to take care of null check if it is not primitive, you know to avoid the famous NPE(NullPointerException) , which is an additional overhead.

There are many such reasons, but I believe you got the answer. :)

dharam
  • 7,882
  • 15
  • 65
  • 93
1
Why operation cost done on wrapper classes will be higher than ??

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

For Example.

Consider a case with int and Integer

While run time

If p is a value of type int, then boxing conversion converts p into a reference r of class and type Integer, such that r.intValue() == p

And vice-versa, this time will be saved in case,if we use Primitives.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    Hey nice answer for the question here http://stackoverflow.com/questions/17489250/how-can-a-string-be-initialized-using/17489410#17489410 – dharam Aug 31 '13 at 06:04
1

Can someone confirm whether this is indeed right?

Yes, one could construct a very crude benchmark to demonstrate that operations on primitives are at least an order of magnitude faster:

// Use a primitive type long
long now = System.currentTimeMillis();
long count = 0;
long total1 = 0;
do {
    for (int i = 0 ; i != 1000 ; i++)
        total1 += 11;
    count++;
} while (System.currentTimeMillis()-now < 2000);
System.out.println("Primitives: "+count+"  x1000 additions completed.");
// Use a wrapper type Long
now = System.currentTimeMillis();
count = 0;
Long total2 = 0L;
do {
    for (int i = 0 ; i != 1000 ; i++)
        total2 += 11;
    count++;
} while (System.currentTimeMillis()-now < 2000);
System.out.println("Wrapper: "+count+"  x1000 additions completed.");

This produces the following results on ideone:

Primitives: 1125085 x1000 additions completed.
Wrapper:     112760 x1000 additions completed.

As you can see, the primitives are at least ten times faster, even in this very crude test where measuring the time takes a significant portion of the time.

why does the operation cost done on wrapper classes will be higher than their primitive counterpart?

This is easy to explain: in addition to the operation itself that is performed on the primitive, the operation on the wrapper requires these operations:

  • Accessing the content of the wrapped type (i.e. another memory read),
  • Construction of a new wrapper object, and
  • Implicitly, unused wrappers from previous runs contribute to the time taken by the garbage collector.

The allocation operation is particularly expensive compared to the arithmetic operation performed on the value inside the primitive.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523