Java关于Integer面试题

来源:互联网 发布:淘宝小号批量手机号 编辑:程序博客网 时间:2024/05/17 18:28
package day13_Integer;/* * JDK5的新特性: * 自动装箱:把基本类型转为包装类类型 * 自动拆箱:把包装类类型转为基本类型 *  * Integer面试题? * 看程序写结果。 * 注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据。 * */public class IntegerDemo4 {public static void main(String[] args) {// TODO Auto-generated method stub//定义一个int类型的包装类类型变量//Integer i = new Integer(100);Integer i=100;i+=200;//可以的System.out.println("i:"+i);System.out.println("------------");//面试题Integer in= new Integer(127);Integer in2 = new Integer(127);System.out.println(in==in2);//falseSystem.out.println(in.equals(in2));//trueSystem.out.println("------------");Integer in3= new Integer(128);Integer in4 = new Integer(128);System.out.println(in3==in4);//falseSystem.out.println(in3.equals(in4));//trueSystem.out.println("------------");Integer in5= 128;Integer in6 = 128;System.out.println(in5==in6);//falseSystem.out.println(in5.equals(in6));//trueSystem.out.println("------------");Integer in7= 127;Integer in8 = 127;System.out.println(in7==in8);//trueSystem.out.println(in7.equals(in8));//trueSystem.out.println("------------");//通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池//如果数据时该范围内的,每次并不创建新的空间}}

原创粉丝点击