• Jdk1.8 之 Integer类源码浅析


     先看一下它的继承、实现关系:
    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

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

  • 相关阅读:
    centos7 安装jdk 1.8
    CentOS查看Java进程并部署jar包
    Mybatis传多个参数(三种解决方案)
    mybatis使用注解往postgresql数据库表insert数据[主键自增]的写法
    在Linux上安装tomcat和JDK
    Linux下部署springboot项目的步骤及过程
    maven安装第三方jar包到本地仓库
    Android 开源动画框架:NineOldAndroids
    Android多国语言的value文件夹命名方式
    C++编程经验-返回局部变量的讨论(转)
  • 原文地址:https://www.cnblogs.com/ly570/p/11257594.html
Copyright © 2020-2023  润新知