Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Auto-boxing/Auto-Unboxing

 
Shameer Subedar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I just wondering of the performance implications of using the auto boxing features. Does it matter if we allow the complier to put the smarts in or should we explicitly do the conversions ourselves ?

I am developing a real-time trading application and performance related issues are very important to us.

On a side note, having read an article in context of C# in regards to auto-boxing, they mentioned not to over use it as it may have performance issues.

Thanks in advance,
Shameer
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the most important question is not who does the conversion, but whether the conversion needs to be done. I've seen a few examples recently of people declaring variables as, e.g., Integer or Boolean when they really mean int or boolean. In complex code, I can see this causing a lot of unncecssary conversions, and I would guess that's what that article was talking about. In general, I recommend always declaring your primitives as primitives unless you know they will only be used in an object context, like the keys in a Map. Another way to head off unintended conversions is to declare your fields, variables and parameters final whenever you can. Of course, these are things we should have been doing all along, for robustness as well as performance, but auto-boxing makes them more relevant than ever.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Alan - if there is a performance problem at all, it's caused by the boxing/unboxing itself, not by it being automatic instead of manual.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic