Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on 

boxing/unboxing/autoboxing

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
Java

Avatar of undefined
Last Comment
Mick Barry
Avatar of for_yan
for_yan
Flag of United States of America image

Avatar of for_yan
for_yan
Flag of United States of America image

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

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Jay Roy
Jay Roy
Flag of United States of America image

ASKER

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

shouldnt it  be  Vector <Integer> vt = new Vector <Integer> ();
with a capital I
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of for_yan
for_yan
Flag of United States of America image

I would think it should be capital, they probably have a miosprint
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> 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.
Avatar of Jay Roy
Jay Roy
Flag of United States of America image

ASKER

>>> 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.
Avatar of for_yan
for_yan
Flag of United States of America image

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.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> 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
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo