A new feature of Java13-5 JDK1.5 and an integer face question

Source: Internet
Author: User

1. New features of JDK5
Auto-boxing: converting a base type to a wrapper class type
Auto-unpacking: converting wrapper class types to basic types

Note a small problem:
When used, Integer x = null; The code appears Nullpointerexception.
Standardization: It is recommended that you first determine whether it is null before you use it.

1  public classIntegerdemo {2  public Static voidmain (string[] Args) {3 //defines a wrapper class type variable i for an int type4 //integer i = new integer (+);5Integer II = 100;6II + = 200;7System.out.println ("ii:" +ii);8 9 //through the post-compilation codeTen //Integer II = integer.valueof (+);//Automatic Boxing one //II = integer.valueof (ii.intvalue () + +);//automatic unpacking and automatic packing a //System.out.println ((new StringBuilder ("ii:")). append (ii). toString ()); -  -Integer III =NULL; the //NullPointerException - if(iii! =NULL) { -III + = 1000; - System.out.println (iii); +}//This is a dead code, never coming in. - } +}

2. Interview Questions:

First Question:
Integer i = 1;
i + = 1;
What does this piece of code do?

Answer: Integer i = 1; Auto Boxing
i + = 1; Automatic unpacking and Automatic packing

The second question:
See how the program writes results
 

1  public classIntegerdemo {2  public Static voidmain (string[] Args) {3Integer I1 =NewInteger (127);4Integer i2 =NewInteger (127);5System.out.println (i1 = =i2);6 System.out.println (i1.equals (i2));7System.out.println ("-----------");8 9Integer i3 =NewInteger (128);TenInteger I4 =NewInteger (128); oneSystem.out.println (i3 = =i4); a System.out.println (i3.equals (i4)); -System.out.println ("-----------"); -  theInteger i5 = 128; -Integer I6 = 128; -System.out.println (i5 = =i6); - System.out.println (i5.equals (i6)); +System.out.println ("-----------"); -  +Integer i7 = 127; aInteger i8 = 127; atSystem.out.println (i7 = =i8); -System.out.println (i7.equals (i8));


The answer Above: the Equals method is true, and the answer to = = Is: Flase,flase,flase,true
By looking at the source code, we know that a data buffer pool is made for data between 128 and 127.
If the data is within that range, extract the data directly from the buffer pool without creating a new space
If the data is not in this range, you will need to recreate the new space
So System.out.println (i5 = = i6); this is Flase.
Integer II = integer.valueof (127);
}
}

Note: integer data is directly assigned, if between 128 and 127, the data is fetched directly from the buffer Pool.

A new feature of Java13-5 JDK1.5 and an integer face question

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.