Hello guys!
Can someone help me with Autoboxing/Unautoboxing? Any simple example should work cause I just started reading about OOP and don't know much about it yet.
Thanks in advance!!
Mel!
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hello guys!
Can someone help me with Autoboxing/Unautoboxing? Any simple example should work cause I just started reading about OOP and don't know much about it yet.
Thanks in advance!!
Mel!
Google has a wealth of examples on autoboxing, so I encourage you to do a quick search to understand it more thoroughly. Here is one example
Integer i = 20;
And Unboxing?
int j = i;
I meant converting it to a normal int from an object Integer.
First, see my post above. Second, go to Bbr's post (note the same variable names). Last but not least: autoboxing unboxing java - Google Search
Why do you want to use boxing?
Sometimes hidden conversions can fool you. Better to write the code needed to do the job.
boxing could be suitable for one off programs like students create and then discard.
I wouldn't use it in a production program that is to be maintained for years by multiple programmers.
Sometimes boxing is necessary (and quite helpful). For example, you can't use primitives as generic parameters.
ArrayList<Integer> myList = new ArrayList<Integer>(); // must use the Integer wrapper class in generics myList.add(1); // 1 is of type int, not Integer. However, it gets auto-boxed so this is perfectly legal
The equivalent code without using auto-boxing (well, technically this is still "boxing", or wrapping, but it's not automatic)
ArrayList<Integer> myList = new ArrayList<Integer>(); myList.add(new Integer(1));
Helpful is good for one off student programs or lazy programmers.
What excuse is there for using hidden compiler tricks that could potentially confuse/mislead some one working on a program?
I just gave a perfectly clear example of why Auto-boxing is useful
From a code maintainability standpoint, the first piece of code I have is much easier to read. In industry, code maintainability is almost always a top priority (depending on the situation, performance sometimes dominates over maintainability, or laziness/time constraints can dominate too).
Also, whenever you use math operations with Integer/wrapper classes, the values are un-boxed to perform the operations (of course, this issue could be circumvented by having overloaded operators, but this is how Java decided to implement it).
So, this prevents you from having to do something like this:
Integer a = new Integer(5); Integer b = new Integer(3); // perform (a + b) * (a - b) // these methods actually don't exist in the Integer class, but hopefully you can tell what it is they are doing // this is actually very similar to how you would have to do this if you were to use BigInteger, but that's a whole other can of worms Integer c = a.add(b).multiply(a.subtract(b));
If you were someone looking at this, wouldn't you prefer (a + b) * (a - b)? It would definitely be much easier to read, and it would also be much easier to write in the first place (you get the benefits of order of operations between operators).
Basically, the thing about auto-boxing is just know it's there (actually, you can even get by and "inadvertently" use this feature without even knowing it exists or what it's called). There's no extra work on your part (actually, quite the opposite, there's loads less work).
This is why people don't generally use assembly all the time, or even worse hand-code out the machine code. It's because some computer scientist came along and decided "this is a lot of work. I wonder what's the easiest (or laziest) way I can get this done?"
Just for fun, here's how this operation would have to be carried out using z80 assembly (a very old assembly language, I know, but it demonstrates the point)
ld a, 5 ld b, 3 add a,b ld d,a ld a,5 ld b,3 sub b ld b,a ld a,d multiplyLoop: add a,d dec b jp nz, mulitplyLoop
Last edited by helloworld922; July 22nd, 2010 at 12:46 PM.
Hello guys,
First THANK YOU ALL! for taking the time to reply! And second I think i'm moving a bit to fast from basic Java to OOP, it's all being to much at once. So I thought about it and I decided to work on mastering what I know before I go in to vectors, JFrame, multi classes and all of the other advanced Java programing things.
If you guys could link me to sites that have none OOP source code for me to play around with, that would be the great(Failed on finding one :/ gl). The only OOP elements I wouldn't mind are inheritance, a bit of vectors would be fine too, I think.
Thanks again and sorry for my late reply!
Java's an all OOP language, so it's fairly difficult to write Java procedurally (not to say it isn't possible, but it's generally not something you would want to do)
In all reality, you only need to know 3 basic concepts about OOP:
1. Abstraction - "It doesn't matter what's actually happening, I just want to know how to use it"
2. Inheritance - "I get everything my ancestors have plus what I have"
3. Polymorphism - "I can look like me, or I can look like my parent/grand parent/etc, but I am still me!"
interfaces fall along the same lines as abstraction.
Last edited by helloworld922; July 26th, 2010 at 06:05 PM.
What I meant by non OOP source code was code with just the basic variables, loops and arrays. I use Java in 24 hours by SAMS publishing, and if you've read the book you must have felt it's to much in a few pages/resources at times. I am not too comfy at all with the basics yet esp loops. About 2 month since I started and I want to play around with what I know before moving on.
I found this not sure it will help being a full course, could be useful after the book. But something I can use along the book would be great!!
Oh and here is are links I found to a site that has free courses on tech related stuff, hope someone can use them!
Free Online Learning at GCFLearnFree.org
Training Courses for Information Technology Professionals - Verhoef Training, USA
Java already has plenty of hidden compiler tricks (String's overloaded operators, the enhanced 'for' loop, anonymous classes, etc).
The thing to watch out for with autoboxing is the (rare) situations where it is important to the code to distinguish between primitive numerics and their wrapper classes.