Quick Reference of autoboxing in JDK 1.5

Source: Internet
Author: User
Tags wrappers

Boxing is simply converting primitive type to Wrapper class. e.g. Integer skip bj = new INTEGER (100). autoboxing is compiler taking care of boxing. With autoboxing following is a legal statement:
Integer required bj = 100;

Vice-versa is called unboxing. JDK 1.5 supports autoboxing and auto-unboxing.

Following is legal with autoboxing
1. integer between bj1 = 100; integer between bj2 = 200;
2. Integer limit bj3 = Limit bj1 + limit bj2;
3. Map. Put ("zero", 0); // map is defined as map
4. Int z = map. Get ("zero ");
5. boolean result = (export bj1 = export bj2 );
6. Int intvalue = Limit bj1; // throws nullpointerexception
7. Foo (intvalue); // foo is defined as-Public void Foo (integer between bj1 ){}
8. Bar (optional bj1); // bar is defined as-Public void bar (INT intvalue ){}
9. If (booleanobj) {}// booleanobj is of type boolean
10. Int intvalue =-required bj1;

Following is illegal
1. Integer parameter bj = shortval; // shortval is of type short
2. Double doubleobj = 10; // can not convert from int to double

Advantage of autoboxing and auto-unboxing is concise code. but careless use might result in poor performance of the Code. certain primitives are cached and autoboxing returns same wrapper objects. this value may depend on JVM used, normally this value are:
1. The boolean values true and false.
2. the byte values.
3. The short and INT values between-128 and 127.
4. the char values in the range '/u0000' to '/u007f '.

It means, if limit bj1 = 127 and limit bj2 = 127 then limit bj1 = Limit bj2 returns true. But, if limit bj1 = 128 and limit bj2 = 128 then limit bj1 = Limit bj2 returns false.

Best practices compiled by Brian pontarelli at http://brian.pontarelli.com/2004/07/15/jdk-5-auto-boxing-best-practices [3]

If you can use primitives do it, if you can't don't.
1. Avoid using Wrapper Classes whenever possible.
2. Never do math with wrappers.
3. Try not to use any of the comparison operators (<,>,<=, >=, ==, etc) with wrappers.
4. Avoid usage in loop statements.
5. Always check for null values.

References:

[1] scjp 5: Chapter 3. API contents (Part-1) http://www.exforsys.com/content/view/1885/362/
[2] autoboxing surprises from j2se 5 http://www.theserverside.com/news/thread.tss? Thread_id = 27129
JDK 5-auto-boxing best practices http://brian.pontarelli.com/2004/07/15/jdk-5-auto-boxing-best-practices/
[4] j2se 5.0 in a nutshell http://java.sun.com/developer/technicalArticles/releases/j2se15/
[5] autoboxing http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
[6] Object Type http://en.wikipedia.org/wiki/Autoboxing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.