[JAVA基础]int 和 Integer 有什么区别?Integer的值缓存范围


[JAVA基础]int 和 Integer 有什么区别?Integer的值缓存范围





转载

一、 int与integer的基本使用对比

int 是基本类型,直接存数值,进行初始化时int类的变量初始为0。
integer是对象,用一个引用指向这个对象,Integer的变量则初始化为null。

   ArrayList al=new ArrayList();
  int n=40;
  Integer nI=new Integer(n);
  al.add(n);//不可以
  al.add(nI);//可以
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

1.1 Java两种数据类型分类

原始数据类型,分为boolean,byte,in,char,long,short,double,float
引用数据类型 ,分为数组,类,接口

1.2 Java为每个原始类型提供了封装类

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

原始类型: booleancharbyteshort,int,long,float,double
封装类类型:Boolean,Character,ByteShortIntegerLong,Float,Double
 
 
  • 1
  • 2

二、 基本解析

2.1 自动装箱:将基本数据类型重新转化为对象

    public class Test {  
        public static void main(String[] args) {  
            //声明一个Integer对象
            Integer num = 9;
            //以上的声明就是用到了自动的装箱:解析为:Integer num = new Integer(9);
        }  
    }  
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

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

 public class Test {  
        public static void main(String[] args) {  
            //声明一个Integer对象
            Integer num = 9;
            //进行计算时隐含的有自动拆箱
            System.out.print(num--);
        }  
    }  
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

对比:

/装箱
Integer num = 10;
//拆箱
int num1 = num;
 
 
  • 1
  • 2
  • 3
  • 4

三、深入解析

3.1 情况描述

   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
        }  
    }  
1
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

解析原因:归结于java对于Integer与int的自动装箱与拆箱的设计,是一种模式:叫享元模式(flyweight)。
加大对简单数字的重利用,Java定义在自动装箱时对于值从–128到127之间的值,它们被装箱为Integer对象后,会存在内存中被重用,始终只存在一个对象。
而如果超过了从–128到127之间的值,被装箱后的Integer对象并不会被重用,即相当于每次装箱时都新建一个 Integer对象。

3.2 Integer源码解析

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

public static Integer valueOf(String s, int radix) throws NumberFormatException {
        return Integer.valueOf(parseInt(s,radix));
    }
 
 
  • 1
  • 2
  • 3
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);
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

  /**
      * 缓存支持自动装箱的对象标识语义
      * -128和127(含)。
      *
      * 缓存在第一次使用时初始化。 缓存的大小
      * 可以由-XX:AutoBoxCacheMax = <size>选项控制。
      * 在VM初始化期间,java.lang.Integer.IntegerCache.high属性
      * 可以设置并保存在私有系统属性中
     */
    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

    <span class="hljs-keyword">static</span> {
        <span class="hljs-comment">// high value may be configured by property</span>
        <span class="hljs-keyword">int</span> h = <span class="hljs-number">127</span>;
        String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty(<span class="hljs-string">"java.lang.Integer.IntegerCache.high"</span>);
        <span class="hljs-keyword">if</span> (integerCacheHighPropValue != <span class="hljs-keyword">null</span>) {
            <span class="hljs-keyword">int</span> i = parseInt(integerCacheHighPropValue);
            i = Math.max(i, <span class="hljs-number">127</span>);
            <span class="hljs-comment">// Maximum array size is Integer.MAX_VALUE</span>
            h = Math.min(i, Integer.MAX_VALUE - (-low) -<span class="hljs-number">1</span>);
        }
        high = h;

        cache = <span class="hljs-keyword">new</span> Integer[(high - low) + <span class="hljs-number">1</span>];
        <span class="hljs-keyword">int</span> j = low;
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> k = <span class="hljs-number">0</span>; k &lt; cache.length; k++)
            cache[k] = <span class="hljs-keyword">new</span> Integer(j++);
    }

    <span class="hljs-keyword">private</span> <span class="hljs-title">IntegerCache</span>() {}
}</code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li><li style="color: rgb(153, 153, 153);">34</li><li style="color: rgb(153, 153, 153);">35</li></ul></pre>                </div>

                                </div>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值