码迷,mamicode.com
首页 > 其他好文 > 详细

Jdk1.8 之 Integer类源码浅析

时间:2019-07-28 09:49:39      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:intvalue   from   hat   占用   大小   run   rop   说明   设置   

 先看一下它的继承、实现关系:
public final class Integer extends Number implements Comparable<Integer>

Number是个抽象类,大概包含六个抽象方法,都是用来类型转换的 具体代码如下:
public abstract class Number implements java.io.Serializable {

public abstract int intValue();

public abstract long longValue();

public abstract float floatValue();

public abstract double doubleValue();

public byte byteValue() {
return (byte)intValue();
}

public short shortValue() {
return (short)intValue();
}

/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -8742448824652078965L;
}
        这就是继承后 子类的实现,强转。
public long longValue(http://www.amjmh.com) {
return (long)value;
}
        看下Comparable 接口,只有一个compareTo 比较大小方法:
public int compareTo(T o);    
            看下Integer的具体        
 public int compareTo(Integer anotherInteger) {
        return compare(this.value, anotherInteger.value);
    }

    public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
    }
      如果看代码有点费劲,那复制如下代码,自己运行下看结果就懂了。
        
public class Test {
public static void main(String[] args) {
Integer it1=128;
Integer it2=-128;
Integer it3=128;
System.out.println(it2.compareTo(it1)+"-----x < y");
System.out.println(it1.compareTo(it2)+"------x > y");
System.out.println(Integer.compare(it1,it3)+"-----x == y");

}
}

       打印结果:
-1-----x < y
1------x > y
0-----x == y

 


2.   Integer的缓存问题

            Integer默认放入缓存的区间是-128 至127 。但最大緩存值可以手動調整,請看:

        The Javadoc comment clearly states that this class is for cache and to support the autoboxing of values between 128 and 127. The high value of 127 can be modified by using a VM argument -XX:AutoBoxCacheMax=size. So the caching happens in the for-loop. It just runs from the low to high and creates as many Integer instances and stores in an Integer array named cache. As simple as that. This caching is doing at the first usage of the Integer class. Henceforth, these cached instances are used instead of creating a new instance (during autoboxing).

         運行以下代碼看效果:
        
public class IntegerTest {
public static void main(String[] args) {
Integer it1=2008;
Integer it2=2008;
System.out.println(it1==it2);//true 说明:为啥超了127了还是true呢?因为我设置了vm arguments参数 -XX:AutoBoxCacheMax=2008
Integer it1t=2009;
Integer it2t=2009;
System.out.println(it1t==it2t);//false 说明:超过 2008 false了吧~~
Integer newit1=new Integer(127);
Integer newit2=new Integer(127);
System.out.println(newit1==newit2);//false 说明:New的话是在heap上占用独立内存的新对象
int it3=127;
int it4=127;
int it5=2008;
System.out.println(it3==it4);//true
System.out.println(it1==it5);//true
}
}
            print result :
        
true
false
false
true
true

        設置虛擬機啓動參數
---------------------

Jdk1.8 之 Integer类源码浅析

标签:intvalue   from   hat   占用   大小   run   rop   说明   设置   

原文地址:https://www.cnblogs.com/ly570/p/11257594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!