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.

Results 1 to 15 of 15

Thread: How can I Autoboxing?

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default How can I Autoboxing?

    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!


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: How can I Autoboxing?

    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;

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: How can I Autoboxing?

    And Unboxing?

  4. #4
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Re: How can I Autoboxing?

    int j = i;

  5. #5
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: How can I Autoboxing?

    I meant converting it to a normal int from an object Integer.

  6. #6
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: How can I Autoboxing?

    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

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: How can I Autoboxing?

    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.

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How can I Autoboxing?

    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));

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: How can I Autoboxing?

    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?

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How can I Autoboxing?

    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.

  11. #11
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: How can I Autoboxing?

    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!

  12. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How can I Autoboxing?

    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.

  13. #13
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: How can I Autoboxing?

    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

  14. #14
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: How can I Autoboxing?

    Quote Originally Posted by Norm View Post
    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?
    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.

  15. #15
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: How can I Autoboxing?

    Please take a look at this thread.
    Array and loops