• java面试题之int和Integer的区别


    int和Integer的区别

    1、Integer是int的包装类,int则是java的一种基本数据类型 
    2、Integer变量必须实例化后才能使用,而int变量不需要 
    3、Integer实际是对象的引用,当new一个Integer时,实际上是生成一个指针指向此对象;而int则是直接存储数据值 
    4、Integer的默认值是null,int的默认值是0

    延伸: 
    关于Integer和int的比较 
    1、由于Integer变量实际上是对一个Integer对象的引用,所以两个通过new生成的Integer变量永远是不相等的(因为new生成的是两个对象,其内存地址不同)。

    Integer i = new Integer(100);
    Integer j = new Integer(100);
    System.out.print(i == j); //false

    2、Integer变量和int变量比较时,只要两个变量的值是向等的,则结果为true(因为包装类Integer和基本数据类型int比较时,java会自动拆包装为int,然后进行比较,实际上就变为两个int变量的比较)

    Integer i = new Integer(100);
    int j = 100;
    System.out.print(i == j); //true

    3、非new生成的Integer变量和new Integer()生成的变量比较时,结果为false。(因为非new生成的Integer变量指向的是java常量池中的对象,而new Integer()生成的变量指向堆中新建的对象,两者在内存中的地址不同)

    Integer i = new Integer(100);
    Integer j = 100;
    System.out.print(i == j); //false

    4、对于两个非new生成的Integer对象,进行比较时,如果两个变量的值在区间-128到127之间,则比较结果为true,如果两个变量的值不在此区间,则比较结果为false

    Integer i = 100;
    Integer j = 100;
    System.out.print(i == j); //true
    Integer i = 128;
    Integer j = 128;
    System.out.print(i == j); //false

    对于第4条的原因: 
    java在编译Integer i = 100 ;时,会翻译成为Integer i = Integer.valueOf(100);,而java API中对Integer类型的valueOf的定义如下:

    public static Integer valueOf(int i){
    assert IntegerCache.high >= 127;
    if (i >= IntegerCache.low && i <= IntegerCache.high){
    return IntegerCache.cache[i + (-IntegerCache.low)];
    }
    return new Integer(i);
    }

    java对于-128到127之间的数,会进行缓存,Integer i = 127时,会将127进行缓存,下次再写Integer j = 127时,就会直接从缓存中取,就不会new了

    简述:int与Integer的区别:

    对于它们,我们可能只是知道简单的区别。Integer是int的一个封装类,int的初始值为0,而Integer的初始值为null。但是他们之间真的仅仅只有这些区别吗?我觉得答案是否定的,于是我决定深入到jdk源码中一探究竟。看看Integer与int到底有什么区别。

    执行代码:

    复制代码
     1 public class IntegerTest {
     2 
     3     public static void main(String[] args) {
     4         // TODO Auto-generated method stub
     5         int intNum = 127;
     6         Integer integerNum = 127;
     7         Integer integerNewNum = new Integer(127);
     8         
     9         //比较两个int变量
    10         int intNum1 = 127;
    11         System.out.print("int与int:");
    12         System.out.println(intNum==intNum1);
    13                 
    14         
    15         //比较int与Integer区别
    16         System.out.print("int与Integer:");
    17         System.out.println(intNum==integerNum);
    18         
    19         //比较int与Integer区别
    20         System.out.print("int与NewInteger:");
    21         System.out.println(intNum==integerNewNum);
    22         
    23         //比较Integer与NewInteger
    24         System.out.print("Integer与NewInteger:");
    25         System.out.println(integerNum==integerNewNum);
    26                 
    27         //比较两个NewInteger
    28         Integer integerNewNum1 = new Integer(127);
    29         System.out.print("NewInteger与NewInteger:");
    30         System.out.println(integerNewNum==integerNewNum1);
    31                 
    32         //比较两个小于128的声明变量
    33         Integer integerNum1 = 127;
    34         System.out.print("小于128的Integer与Integer:");
    35         System.out.println(integerNum==integerNum1);
    36         
    37         //比较两个大于等于128的声明变量
    38         Integer integerNum2 = 128;
    39         Integer integerNum3 = 128;
    40         System.out.print("大于等于128的Integer与Integer:");
    41         System.out.println(integerNum2==integerNum3);
    42         
    43     }
    44 
    45 }
    复制代码

    运行结果:

    复制代码
    int与Integer:true
    int与NewInteger:true
    Integer与NewInteger:false
    int与int:true
    NewInteger与NewInteger:false
    小于128的Integer与Integer:true
    大于等于128的Integer与Integer:false
    复制代码

    问题:

    1.为什么当我们使用数值相等的integerNum、integerNewNum与intNum比较时结果为true?

    2.为什么当我们使用数值相等的integerNum与integerNewNum进行比较时结果为false?

    3.为什么integerNum与integerNum进行比较时会出现大于128和小于等于128不同结果的情况?

    我的理解:

    一下所有讨论问题的前提是:两个int型变量所赋值的数值相同时,比较结果为true,即12行的结果。

    1.17行与21行所得到的结果为true,其实我们如果我们从源码来理解就会知道其本质了。在此之前我们应该先补充一个感念,Integer integerNum =127在执行时会被翻译成

    Integer integerNum = Integer.valueOf(127)。源码如下

    复制代码
    1 public static Integer valueOf(String arg) throws NumberFormatException {
    2         return valueOf(parseInt(arg, 10));
    3     }
    4 
    5     public static Integer valueOf(int arg) {
    6         return arg >= -128 && arg <= Integer.IntegerCache.high ? Integer.IntegerCache.cache[arg + 128]
    7                 : new Integer(arg);
    8     }
    复制代码
    复制代码
     1 private static class IntegerCache {
     2         static final int low = -128;
     3         static final int high;
     4         static final Integer[] cache;
     5 
     6         static {
     7             int arg = 127;
     8             String arg0 = VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
     9             int arg1;
    10             if (arg0 != null) {
    11                 try {
    12                     arg1 = Integer.parseInt(arg0);
    13                     arg1 = Math.max(arg1, 127);
    14                     arg = Math.min(arg1, 2147483518);
    15                 } catch (NumberFormatException arg3) {
    16                     ;
    17                 }
    18             }
    19 
    20             high = arg;
    21             cache = new Integer[high - -128 + 1];
    22             arg1 = -128;
    23 
    24             for (int arg2 = 0; arg2 < cache.length; ++arg2) {
    25                 cache[arg2] = new Integer(arg1++);
    26             }
    27 
    28             assert high >= 127;
    29 
    30         }
    31     }
    复制代码
    复制代码
    1 private final int value;
    2 public Integer(int arg0) {
    3         this.value = arg0;
    4     }
    5 
    6     public Integer(String arg0) throws NumberFormatException {
    7         this.value = parseInt(arg0, 10);
    8     }
    复制代码

    以上jdk源码可以得到,Integer 无论是声明还是新建对象,最终所得的值都是int型,所以我们问题1就等到了答案。两个int型比较所得结果必然是ture.

    2.25行所得结果为false,为什么呢?不是都转成int型嘛?结果不是应该为false嘛?额额额额。new了地址就不一样了所以当我们使用"=="进行比较时尽管数值相同,但内存地址却早已不相同。这就涉及到了下一篇博文中会讲的"=="与".equals"的区别。

    3.为什么会出现问题3这种情况,128会是一个零界点呢?其实答案在源码中我们也可以找到

    复制代码
    1 public static Integer valueOf(String arg) throws NumberFormatException {
    2         return valueOf(parseInt(arg, 10));
    3     }
    4 
    5     public static Integer valueOf(int arg) {
    6         return arg >= -128 && arg <= Integer.IntegerCache.high ? Integer.IntegerCache.cache[arg + 128]
    7                 : new Integer(arg);
    8     }
    复制代码

    当arg大于等于-128且小于等于127时则直接从缓存中返回一个已经存在的对象。如果参数的值不在这个范围内,则new一个Integer对象返回。

    以上就是int与Integer的区别,我们再日常的项目或者练习中经常拿不准该使用int还是Integer,但看了这篇博文你应该已经有了自己的答案了吧!

  • 相关阅读:
    yaf(3) 正则路由
    yaf(1) 配置文件
    PHP 分页URL设计
    smarty模版目录疑问
    yaf(5) smarty
    yaf(2) layout 自定义目录结构
    php 倒计时出现0的情况
    PHP 单一入口框架设计简析
    利用jquery.load()实现html框架效果
    yaf(4) Yaf_Loader()
  • 原文地址:https://www.cnblogs.com/aspirant/p/10189604.html
Copyright © 2020-2023  润新知