As you may know auto boxing and unboxing is a new feature of the Java 6.(which is not new for languages like C#, we can say it is new feature for the Java).Java has two main data types one is primitive type and other is reference types.Primitive types are the types generally built in language (like int , short,byte,long,float,double,bool,char…) and allocated on the stack side of the memory.But reference types are the types lets say shortly any type that subtype of the class Object in java (like class,array,String…) and they allocated on the heap side of the memory.
What about boxing and unboxing , boxing is conversion of the one primitive type to its corresponding reference type (int to Integer in this situation) while unboxing is conversion of the reference type to corresponding primitive type.You can think of actually “boxing” the varaible and putting it to heap side , and unboxing as reverse procedure to the stack side.
These boxing and unboxing operations are automatically done by java while using generics.Generics can think of as a compile time check for class or interface or any other types that used with generics for their parameters.From definition its hard to understand so lets show a example of generics use.
1 2 3 | List<Integer> ints = new ArrayList<Integer>(); ints.add(1); int n = ints.get(0); |
Here as you can see we are creating a list from arraylist(which is a class the implements list interface so assigment is OK) with specified parameters String in angle brackets.With the help of generics it is guaranteed that all the parameters that added to arraylist is String , if you try to add something other that String it is a compile time error.Before java generics its programmer job to guess what is the variable type coming from list and make suitable cast to that object.It is a extra work for programmer.With generics its is now compiler responsibility to check parameters and give compile time error.
You can say it is not different from c++ templates , but actually it does.There is a different implementation of the generics in bot c++ and java side.In C++ if you use generics and create and objects lets say List<String> and List<Integer> and List<List<String>> , C++ made 3 copy of your code for each generic in use.If you use 100 different type of generics there will be 100 different code , this can lead to code bloat.But in java no matter how many differnt generics you use there is only one code for all.C++ model generics called expansion,it can be lead to code bloat but in performance side it is more efficient that Java model.Also if you are using generics with C++ you have to be careful about usage of angle brackets.
Lets look at another code to see how auto boxing and unboxing work with Java.
1 2 3 4 5 | public static int sum (List<Integer> ints) { int s = 0; for (int n : ints) { s += n; } return s; } |
In this code sample we again using a list from arraylist as parameter to sum function with specified Integer generic.(you can say what is the difference between “Integer” and “int” , and why we not using “int” for generics like this “List<int> ints”) Cause type parameters always bound to reference type not primitive types.And also you can ask why we are using “int” insted of “Integer” as return type like “return Integer”? .Because result types may be either primitive or reference types, and it is more efficient to use the former than the latter.Also you can say why for variable(or you can say foreach another new feature of java 6) not “Integer” insted of “int”.This is actually hold performace issue behind.If you use “Integer” instead of “int” compiler unbox the value of “Integer” every time and perform operation than box the value back for later iteration.This is a big performance loss.
Here how generics doing auto boxing and unboxing.
1 2 3 | List<Integer> ints = new ArrayList<Integer>(); ints.add(new Integer(1)); int n = ints.get(0).intValue(); |
This is the different version of the first code sample at top.This is how it looks when we are using generics in our code.As you can see if we try to add int value to List that expects Integer , it automatically box the value and then add it to the list.On the other hand if we try to get the value out of list , it again automatically unbox the value and assign it .(here we using integer so if we try to box int value it is done with new Integer(x) , and if we try to unbox it you have to call intValue method of the Integer).
For more information about generics you can look at Sun documentation for generics and also for Collections framework of Java.


This is exactly what I was searching for on bing, I guess I got my answer!
Kilian Engert
1 Jun 10 at 18:45
int i = 10;
Integer intObj = 10; //auto boxing
(or) Integer intObj = i;
i = new Integer(5); // unboxing
Arun
2 Jun 10 at 08:14
Seriously though, you can easily make that image from scratch on PS, so I wouldn’t count it as a “photo”
Elwood
5 Jun 10 at 03:22
Thanks for sharing
Freddie Salstrom
6 Jun 10 at 06:26
Your work has always been a great source of inspiration for me. I refer you blog to many of my friends as well.
johnhoma124
6 Jun 10 at 23:34
This is a very informative read and I totally understand where your coming from in the fourth section. Perfect insight, Ill be sure to read this blog’s other articles.
Deanna Ellender
7 Jun 10 at 03:07
Yeah, I am quite interest in your domain. If I set up one website using this domain, I can earn 100-200 USD daily. Your domain is really good. If you don’t want to sell this domain, I can cooperate with you on the condition that you share 50% revenue with me.
shaaira buckner
7 Jun 10 at 13:37
Awesome article, thank you for posting this.
cartuccia toner
13 Jun 10 at 00:27
I just wanted to say that I found your site via Goolge and I am glad I did. Keep up the good work and I will make sure to bookmark you for when I have more free time away from the books. Thanks again!
Cavenzi
13 Jun 10 at 10:46
Hei, I like this blog, many good information, I want to subscribe it, can anyone help me?
Online Travel Business
6 Jul 10 at 02:41
I completely agree with everything in this post. keep up the good work
Babette Uhles
7 Jul 10 at 22:27
Hello, with the abundance of crappy blogs around it’s great to see that there are still some filled with great information! Is there any way I can be emailed when you create a new post? thanks!
Viki Reagans
7 Jul 10 at 23:31
Is that a fact or theory? It was a good read
Business Solutions
11 Jul 10 at 02:24
HA! I am quite interested in your site. If I were to use this website, I could earn you 100-200 USD daily. I am willing to work on monetizing your site, on the condition that you share 50% of revenues with me. If you are interested, please send me an email.
Issac Maez
11 Jul 10 at 06:20
Being a new blogger, I would like to tell you that you have given me much knowledge about it. Thanks for everything.
Mr.parts tools
parts tools
11 Jul 10 at 10:28
Excellent along with very thoughtful post! My partner and i really appreciate the particular time along with effort you put in every single one of your posts, along with finally decided to drop your comment on one of them! I’ve shared your site with my other blogger friends, and also subscribed so hopefully you do the same for me.
xbox live code generator
12 Jul 10 at 10:19
The articles you have on your blog are always so enjoyable to read. Good work and I bookmarked it.
abc kids games
12 Jul 10 at 12:51
That may be some inspirational stuff. Never knew that opinions may very well be this varied. Thanks for all of the enthusiasm to present such beneficial data the following.
1 year fixed rate
15 Jul 10 at 06:34
Really great informative blog post here and I just wanted to comment & thank you for posting this. I’ve bookmarked youi blog and I’ll be back to read more in the future my friend! Also nice colors on the layout, it’s really easy on the eyes.
Seo Services
28 Jul 10 at 05:44
I just search some keywords at Google and then I enter here, I find many information, and now I just want to let you know I really like to read your posts!
free full downloadable movies
4 Aug 10 at 02:58
Document admire this valuable data you offer in your articles. My spouse and i will bookmark your website along with have my children check up here often. My spouse and i am quite sure they will learn lots of new stuff here than anybody else!
xbox live code generator
11 Aug 10 at 15:37
Advantageously, the post is in reality the greatest on this deserving topic. I harmonise with your conclusions and will eagerly look forward to your incoming updates. Saying thanks will not just be sufficient, for the tremendous clarity in your writing. I will right away grab your rss feed to stay informed of any updates. Good work and much success in your business enterprize Boxing Forums is the best place to find Stock Market Forum news.
Boxing news
12 Aug 10 at 16:07
good!it’s very useful!thx!
Micheal Fredrikson
12 Aug 10 at 17:29
Very inspirational post! Thank you!
Doradztwo kredytowe
12 Aug 10 at 22:43
I apologize for the possible off-topic Guys, but could you inform me which web host you are using right now I have a problem because my current web hosting company godaddy.com is kicking me off. They say that our website is using too many server resources. But our website only gets 100 visitors per week. Anyway, what would be your recommendations? Are you happy with your web host? I am checking LCN at the moment, but is it real that they can provide unlimited disk space? I have contacted them and they say something like 2tb disk drive costs only $20 per month, so if some day we will start running out of disk space we will simply add a new 2tb drive so can continue uploading.. Whats your opinion? Any ideas and web host name suggestions are much appreciated.
Trance
15 Aug 10 at 01:06
This is seriously horrifying – I had no idea! I really don’t like the loss of that beautiful old-fashioned language. it genuinely upsets me to think of them not reading it all in the original. Thanks for the heads up!
Merle Brophy
17 Aug 10 at 02:17
Hi, it’s a very good article, and very nice info.come and see my blog.
site
17 Aug 10 at 17:40
Took me time to understand all the remarks, however I genuinely liked the post. It turned out to be Quite valuable to me and I am sure to almost all the commenters here! It’s definitely nice when you can not merely be educated, but also amused! I’m sure you had fun creating this kind of article.
Snow Cone Syrup
18 Aug 10 at 05:18
i am using hostgator and they are very good at what they are doing
burakdede
30 Aug 10 at 12:26