Java Fundamentals Hardening 68: Face questions for the integer direct assignment of the basic type wrapper class

Source: Internet
Author: User

1. Interview questions:

Integer i = 1;

i + = 1;

What has been done?

(1) where Integer i = 1; automatic boxing (using valueof () method, int---> Integer)

(2) where I +=1; the integer type I is automatically disassembled into int (using the Intvalue () method, Integer--->int), after the completion of the addition run I is then boxed into an integer type.

2. Interview questions:

Observe the following procedure to determine the output to write the result:

1  Packagecn.itcast_06;2 3 /*4 * See program Write results5  * 6 * Note: integer data is directly assigned, if between 128 and 127, the data will be fetched directly from the buffer pool.7  */8  Public classIntegerdemo {9      Public Static voidMain (string[] args) {TenInteger I1 =NewInteger (127); OneInteger i2 =NewInteger (127); ASystem.out.println (I1 = =i2); - System.out.println (I1.equals (I2)); -System.out.println ("-----------"); the  -Integer i3 =NewInteger (128); -Integer I4 =NewInteger (128); -System.out.println (i3 = =I4); + System.out.println (I3.equals (I4)); -System.out.println ("-----------"); +  AInteger i5 = 128; atInteger I6 = 128; -SYSTEM.OUT.PRINTLN (i5 = =I6); - System.out.println (I5.equals (I6)); -System.out.println ("-----------"); -  -Integer i7 = 127; inInteger i8 = 127; -System.out.println (i7 = =i8); to System.out.println (I7.equals (i8)); +  -         //by looking at the source code, 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 the         //Integer II = integer.valueof (127); *     } $}

The results of the program run:

Java Fundamentals Hardening 68: Face questions for the integer direct assignment of the basic type wrapper class

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.