boxing/unboxing/autoboxing

royjayd
royjayd used Ask the Experts™
on
hi guys

I would like to have some information about what is boxing/unboxing/autoboxing with a simple example.
Interms of application performance is it good to use them or avoid them?
How is this related to generics in java?

A simple and clear answer would be greatly appreciated

thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011

Commented:
Awarded 2011
Awarded 2011

Commented:
One of the examples in there  shows how it is related to generics

Consider the following example: if we want to store an int “a” into vector “vt”, we have to use wrapper class. And if we want to get element stored at position “0” of vector “vt”, we again have to do casting.

 	int a = 10;
 
 	Vector vt = new Vector();
 
 	vt.add(new Integer(a));
 
 	int n = ((Integer)vt.elementAt(0)).intValue();

J2SE 5.0 has made this easy. If we want to do the same in Java 5, the code will be like:

	int a = 10;
 
	Vector <integer> vt = new Vector <integer> ();
 
	vt.add(a);
 
	int n = vt.elementAt(0); 

Open in new window

Awarded 2011
Awarded 2011
Commented:
And this is about performance and other issues - still from the same text:

Few things to remember when using autoboxing/unboxing is that java compiler actually manages type conversions for us. So boxing and unboxing too many values can make garbage collector go wild. Hence it is not a advisable to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code as it will affect the performance to a good extent.Using primitive types will better serve the purpose there.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Author

Commented:
>>>Vector <integer> vt = new Vector <integer> ();

shouldnt it  be  Vector <Integer> vt = new Vector <Integer> ();
with a capital I
Mick BarryJava Developer
Top Expert 2010
Commented:
yes it should
Awarded 2011
Awarded 2011

Commented:
I would think it should be capital, they probably have a miosprint
Mick BarryJava Developer
Top Expert 2010

Commented:
> Interms of application performance is it good to use them or avoid them?


avoid it.

All its doing is create a wrapper Object for you. And too much Object creation is going to hurt the performance of your application.

Author

Commented:
>>> So boxing and unboxing too many values can make garbage collector go wild. Hence it is not a advisable to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code as it will affect the performance to a good extent.Using primitive types will better serve the purpose there.

We could not use primitive types directly to add to  a  Collection and that is why they came out with autoboxing/unboxing. so i dont understand what they mean by
>>Using primitive types will better serve the purpose there.
Awarded 2011
Awarded 2011

Commented:
Of course all complications apply if you have huge amounts of data.
If you have arrays data in hundreds and maybe not too big thousands  convenience is of couse more important and you can do
it as conevenient for you to prorgram
When it goes to big numbers then you should become cautious.
Mick BarryJava Developer
Top Expert 2010

Commented:
> We could not use primitive types directly to add to  a  Collection and that is why they came
> out with autoboxing/unboxing. so i dont understand what they mean by

thats not really why they came out with autoboxing. Its just "syntactic sugar". They don't allow you to do anytrhing you couldn't do previously, it just makes your code more concise

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial