int和integer的相同点跟不同点

int和integer的区别

int

int是基本数据类型 ,他不是类,他没有方法。

integer

integer是int的包装类 他是一个类 他有自己的方法 可以调用

区别int和integer输出结果的对比

package test;

public class text {

	public static void main(String[] args) {
	  int a=100;
	  int b=100;
	  int c=200;
	  int d=200;
	  Integer m=100;
	  Integer n=100;
	  Integer g=200;
	  Integer h=200;
	  System.out.println(a==b);
	  System.out.println(c==d);
	  System.out.println(m==n);
	  System.out.println(a==m);
	  System.out.println(g==h);
	}

}

输出结果:

true
true
true
true
false

int和int类型进行等值比较的进行的是值的比较

int和integer进行等值比较,会进行分割 将integer 分割成int 数值 还是进行数值的比较

integer和integer进行等值比较

integer类中有一个方法valueof对所赋的值进行判断是否进入缓存区

/**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

当赋的值在-128-127之间数据将会进入缓冲区直接进行赋值

当赋的值不在-128-127范围之外将会重新new 一个对象 进行对比

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值