It's not a secret anymore!*
The moose likes Java in General and the fly likes autoboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "autoboxing" Watch "autoboxing" New topic
Author

autoboxing

Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
Do I have autoboxing correct? Does autoboxing automatically create objects out of primitives such as int?

For example, if I had a method that required an Integer object as a parameter, could I pass a normal int to it instead?


If this is the case, how does autoboxing affect performance?


Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Jason,
Yes, your understand is correct. Autoboxing doesn't really affect performance because you would have had to create the Integer yourself anyway.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Autoboxing is reduce your code that it's useful for programmer/developer.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Jason,

With autoboxing/unboxing feature, you don't need to manually create wrapper to encapsulate a primitive value.



Though with this convenience, we shouldn't use it where wrapper objects are not required.

Example of misusing this feature:


Joyce
[ October 20, 2004: Message edited by: Joyce Lee ]
Brett McLaughlin
author
Ranch Hand

Joined: Sep 01, 2004
Posts: 30
Autoboxing is the process of converting a Java primitive to a Java wrapper class -- for example, going from an int to an Integer.

Autounboxing is the opposite -- going from Integer to int.

And the "auto" part of that is because Tiger does it for you -- you can just let the JVM do the work.

That said, and as I point out in my book, there are some gotchas to watch out for. For example, wrapper classes can be assigned the value null, and this will create a NullPointerException if you try to assign a null-valued wrapper to its primitive (via unboxing).

Hope this helps some
Brett


Series Editor, Head First<br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596102259/newinstance-20" target="_blank" rel="nofollow">Head Rush Ajax</a> and <a href="http://www.amazon.com/Head-First-Object-Oriented-Analysis-Design/dp/0596008678/ref=pd_bbs_sr_1/104-5348268-5670331?ie=UTF8&s=books&qid=1192568453&sr=8-1/newInstance-20" target="_blank" rel="nofollow">Head First OOA&D</a>
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
Yes Brett, that does help.

That said, and as I point out in my book, there are some gotchas to watch out for. For example, wrapper classes can be assigned the value null, and this will create a NullPointerException if you try to assign a null-valued wrapper to its primitive (via unboxing).


So, would you say it is good practice to place any unboxing in a try/catch or to explicitly throw the NPE as opposed to ignoring it?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
In general, you shouldn't be catching run-time exceptions, such as NullPointerExceptions, unless you have a very good reason to do so. If your system is critical and absolutely must not go down, for example, you might want to think about catching all Exceptions, not just checked exceptions.

If you ever have a situation where you're going to use auto-unboxing to convert a wrapper into a primitive and that wrapper may be assigned a null value, I think you'd be better off checking for such a situation. In a real-world situation, you may have some special procedure to go through in such a case.



In my opinion, it would be better to check for such a case explicitly rather than to start catching runtime exceptions. I suppose it's probably dependent upon the context in which the situation arises, but, in my opinion, it's generally best to NOT catch runtime exceptions.


SCJP Tipline, etc.
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
I typically do exactly what you suggest unless the program is a webapp and should recover from those exceptions.

thanks Corey.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: autoboxing
 
Similar Threads
autoboxing!
Overriding Doubt
Is-A Relationship and Autoboxing
is-a relationship and autoboxing.
Q on Integer()