An overview of the integer and auto-unboxing of the first season of JAV entry

Source: Internet
Author: User

/* int is just a basic type. int has the corresponding class type, that is integer.
* For more operations on basic data types and more convenient operation, Java provides the corresponding class type-wrapper class type-for each basic data type. Each basic type assigns an operation that encapsulates the corresponding class type to make the basic type data more convenient.
* ByteByte
* Short Short
* INT Integer (this looks very special)
* Long Long
* Float Float
* Double Double
* Char Character (this looks very special)
* Boolean Boolean
*
* Used for conversions between basic data types and strings.

*/

New features of JDK5, automatic disassembly box.

/* * JDK5 new features * Auto Boxing: Converting a base type to a wrapper class type    * Automatic unpacking: Converting wrapper class type to base type   *  */public class Integerdemo {public static void main (string[] args) {//defines a wrapper class type variable of type int i//integer i = new integer, Integer II = 100;//equivalent to the previous sentence, automatically converts 100 to the referenced Integer type II + = 200;//itself to different Type addition will error, and there is no error. This will not be an error, because the Automatic II into the int type is calculated. System.out.println ("II:" + II);//Through the anti-compilation code, the preceding three sentence is equivalent to the following three words//Integer II = integer.valueof (100); Auto-boxing//II = integer.valueof (Ii.intvalue () + 200); Automatic unpacking Ii.intvalue (), then automatically boxed integer.valueof (Ii.intvalue () + +);//System.out.println ((New StringBuilder ("II:")). Append (ii). toString ()); The result is a string type
Attention:
* When using the new JDK feature this way, Integer  x = null; The code will appear nullpointerexception. Null pointer exception * It is recommended that you first determine whether it is null before you use it. if (iii! = NULL) {}
The code is as follows:

Integer III = null;//nullpointerexceptionif (iii! = NULL) {//Add judgment logic, this exception will not occur. III + = 1000; SYSTEM.OUT.PRINTLN (iii);}

Another attribute of the integer class: integer data is directly assigned, and if between 128 and 127, the data is fetched directly from the buffer pool.

By de-compiling, we know that for data between 128 and 127, a data buffer pool is made, and if the data is within that range, no new space is created each time (new addresses are not created)




An overview of the integer and auto-unboxing of the first season of JAV entry

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.