其他分享
首页 > 其他分享> > int 和 Integer的区别

int 和 Integer的区别

作者:互联网

问题:int 和 Integer 有什么区别,Integer的值缓存范围?

一、int 和 Integer 的基本对比

int 是基本数据类型,直接存数值,进行初始化时 int 类的变量初始值为 0 ; Integer 是对象,用一个引用指向这个对象,Integer 的变量初始化值为 null 。 如下代码:

 

ArrayList a = new ArrayList();
int n = 4;
Integer n1 = new Integer(n);
a.add(n);//报错,编译不通过。
a.add(n1);// 编译通过,可以add

①.Java 的两种数据类型分类

原始数据类型:分别:boolean、byte 、short、char、int、float、double、long; 引用数据类型,分为数组 和 接口。

②. Java 为每个原始类型提供了封装类 为了编程开发的方便,Java 还是引入了基本数据类型,但是为了能够将这些基本数据类型当成对象操作,Java 为每一个基本数据类型都引入了对应的 包装类型(wrapper class),int的包装类就是 Integer,从 Java5 开始引入了自动装箱和自动拆箱,使得二者可以相互转换。

原始数据类型:boolean 、char、byte、short、int、long 、float、double 封装类类型: Boolean、Character、Byte、Short、Integer、Long、Float、Double

二、基本解析

① 自动装箱:将基本数据类重新转换为对象

 

public class Test{
 public static void main(String[] args){
       // 声明一个Integer 对象
       Integer num = 10;
       // 以上的声明就是用到了自动的装箱:
       //解析为 Integer num = Integer.valueOf(10);
       //valueOf(参数)方法其实调用的是 new Integer(10);
}
}

10是属于基本数据类型的,原则上它是不能直接赋值给一个对象 Integer的,但是在 JDK5 以后就可以进行这样的声明,自动将数据类型转化为对应的封装类,成为一个对象以后就可以调用对象所声明的所有的方法。

②自动拆箱:将对象重新转换为基本数据类型

 

public class Test{
     public static void main(String[] args ){
       // 声明一个Integer 对象
       Integer  a  = 10;
        // 下面进行计算时隐含的有自动拆箱
       System.out.println( a--);
    }
}

因为对象时不能直接进行运算的,而是要转化为基本数据类型才能够加减乘除。

三、深入分析

①、描述

 

public class Test {  
   public static void main(String[] args) {  
       //在-128~127 之外的数
       Integer num1 = 128;   Integer num2 = 128;          
       System.out.println(num1==num2);   //false

       // 在-128~127 之内的数
       Integer num3 = 9;   Integer num4 = 9;  
       System.out.println(num3==num4);   //true
  }  
}  

分析原因:归结于Java对于Integer 与 int 的自动装箱和拆箱的设计,是一种模式:叫 享元模式(flyweight)。 加大对简单数字的重利用,Java 定义在自动装箱时对于值在 -128~127 之间的值,他们被装箱为Integer 对象后,会存在内存中被重用,始终只有一个对象。

②Integer 源码解析

给一个 Integer 对象赋一个 int 值得时候,会调用 Integer 类的静态方法 valueOf,源码如下:

 

  public static Integer valueOf(String s , int  radix) 
               throws NumberFormatException{
      return Integer.valueOf(parseInt(s,radix));
}
// 在 -128~127 之间从缓存里取
public static Integer valueOf(int i){
       if( i >= IntegerCache.low && i <= IntegerCatche.high){
             return IntegerCache.cache[i +(-IntegerCache.low)];
      }
       return new Integer(i);
}

IntegerCache 是 Integer 内部类,源码如下:

 

  private static class IntegerCache{
     static final int low = -128;
     static final int high;
     static final Integer cache[];
     
     static {
         // high value may be configure by property
         int h = 127;
         String integerCacheHighPropValue = sun.misc.VM.getSaveProperty("java.lang.IntegerCache.high");
         if(integerCacheHighPropValue != null) {
                 try{
                       int i=parseIn(integerCacheHighPropValue);
                       i = Math.max(i,127);
                       h = Math.min(i,Integer.MAX_VALUE -(-low)-1);
                }catch(NumberFormat nfe){
                       // if the propvalue cannot be pared into an int,ignore it
                  }
              }  
                  high = h;
                  cache = new Integer[(high - low)+1];
                  int j = low;
                 for(int k = 0 ; k< cache.length ; k ++)
                       cache[k] = new Integer(j++);
                 
               assert IntegerCache.high >= 127;
        }
         private IntegerCache(){}
         
     
}

标签:区别,int,数据类型,static,Integer,public,IntegerCache
来源: https://www.cnblogs.com/czhai/p/15831608.html