码迷,mamicode.com
首页 > 编程语言 > 详细

Java基础知识强化68:基本类型包装类之Integer直接赋值的面试题

时间:2015-09-24 21:18:23      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

1. 面试题:

Integer  i = 1;

i += 1;

做了哪些事情?

(1)其中Integer i =1;做了自动装箱( 使用valueOf()方法,int ---> Integer )

(2)其中i +=1;先将Integer类型的 i 自动拆箱成 int(使用intValue()方法,Integer--->int ),完成加法运行之后的 i 再装箱成Integer类型。

 

2. 面试题:

观察下面程序,判断输出写出结果:

 1 package cn.itcast_06;
 2 
 3 /*
 4  * 看程序写结果
 5  * 
 6  * 注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据
 7  */
 8 public class IntegerDemo {
 9     public static void main(String[] args) {
10         Integer i1 = new Integer(127);
11         Integer i2 = new Integer(127);
12         System.out.println(i1 == i2);
13         System.out.println(i1.equals(i2));
14         System.out.println("-----------");
15 
16         Integer i3 = new Integer(128);
17         Integer i4 = new Integer(128);
18         System.out.println(i3 == i4);
19         System.out.println(i3.equals(i4));
20         System.out.println("-----------");
21 
22         Integer i5 = 128;
23         Integer i6 = 128;
24         System.out.println(i5 == i6);
25         System.out.println(i5.equals(i6));
26         System.out.println("-----------");
27 
28         Integer i7 = 127;
29         Integer i8 = 127;
30         System.out.println(i7 == i8);
31         System.out.println(i7.equals(i8));
32 
33         // 通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间
34         // Integer ii = Integer.valueOf(127);
35     }
36 }

程序运行的结果:

技术分享

Java基础知识强化68:基本类型包装类之Integer直接赋值的面试题

标签:

原文地址:http://www.cnblogs.com/hebao0514/p/4836394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!