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
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值