• Post Reply Bookmark Topic Watch Topic
  • New Topic

why should I avoid autoboxing  RSS feed

 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I want to ask for some input. Other than autoboxing is performance hog and evil , why should I avoid autoboxing?

Thank you in advance,
Garry
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Autoboxing is neither evil, nor a performance hog, nor should you avoid it. Java does some amazing stuff to make allocating small objects (like Integer, Double, etc) very cheap, and in fact the most recent JVMs can allocate them on the machine stack in many cases.

Like everything else that happens automatically, it pays to understand it and give a little thought to what's happening behind the scenes. But there's really no reason to avoid autoboxing; whoever told you otherwise was acting on superstition rather than knowledge.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If I had to take a guess, I would say that this "superstition" comes from someone who was using Java way before Java 5. I'll even admit that, for a long time, I didn't see much value in it myself. To me, converting a primitive to a wrapper was "muscle memory", and hence, really quick. And has little value for me.

In fact, I still have a problem with newbies who treat the wrappers like primitives, and leave a trail of objects in a garbage every time they do a math calculation.


Over the years, I have gotten over it. And have grown to appreciate it, when it is used with other features, such as in a var-args of objects -- letting the var-arg deal with the number of parameters, and letting autoboxing help make all the types objects.

Henry
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the responses.

Here is what is read on java.sun.com

So when should you use autoboxing and unboxing? Use them only when there is an “impedance mismatch” between reference types and primitives, for example, when you have to put numerical values into a collection. It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code.


I translated the last sentence as what I have mention on my post above.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garry Ba wrote:Thank you for the responses.

Here is what is read on java.sun.com

So when should you use autoboxing and unboxing? Use them only when there is an “impedance mismatch” between reference types and primitives, for example, when you have to put numerical values into a collection. It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code.


I translated the last sentence as what I have mention on my post above.


But "performance hog and evil" is a poor translation of that last sentence. It's like translating "Be careful when you cross a busy street" into "Don't cross any street without a police escort".
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will say, Gary, that the CattleDrive has made me aware that I didn't know how to NOT autobox. Really. I learned Java in this Java6 world and had no idea what was actually happening.

I think it's good to understand how something is working, even when the computer does it for you.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reminds me of how "use the HTTP session wisely" gets turned into "don't use the session".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!