int 和 Integer的区别

int是基本类型,默认值为0,int a=5;a只能用来计算,一般作为数值参数。

Integer是引用类型,默认值为null, Integer b=5;b是一个对象,它可以有很多方法,一般做数值转换,WEB开发中用。

 应用:list,map中存放的是object,所以不能使用基本数据类型,只能使用引用。

package com.wangcf;

public class Test {
    /**
     * 俩个Integer都不是new出来的则可以相等,但是必须在-128到127之间
     * int 和Integer不论是否new都相等
     * 只要有new出来的Integer就不会相等
     * @param args
     */
    public static void main(String[] args) {
        int i=1;
        Integer i1=1;
        Integer i5=1;
        Integer i6=1;
        Integer i7=1;
        Integer i2=new Integer(1);
        Integer i3=new Integer(1);
        Integer i4=new Integer(1);
        System.out.println(i==i2);  //true
        System.out.println(i==i1);    //true
        System.out.println(i1==i5); //true 必须在-128到127之间为true,否则为false
        System.out.println(i1==i2);    //false
        System.out.println(i2==i3);    //false
        System.out.println(i3==i4); //false
        System.out.println(i6==i7); //false
    }
}
原文地址:https://www.cnblogs.com/-beauTiFul/p/6437471.html