zoukankan      html  css  js  c++  java
  • Java | JDK8 | Integer

    Integer 继承 抽象类Number和实现 Comparable<Integer>接口,

    抽象类Number:提供拆箱的超类,可转换的基本类型有 {@code byte}, {@code double}, {@code float}, {@code int}, {@code long}, and {@code short}.

    Comparable<Integer>接口:提供Integer类实现自身比较。

    Integer类的最大值最小值

    @Native public static final int   MIN_VALUE = 0x80000000;
    @Native public static final int   MAX_VALUE = 0x7fffffff;

    最小值 -2^31,最大值2^31

    Integer类的缓存

      Integer的缓存值从-127开始,默认是到127,在static静态代码块进行初始化,在源码中可以看出缓存值的上限high是可以设置

    static final int low = -128;
    static final int high;
    static final Integer cache[];
     static {
         int h = 127;
                String integerCacheHighPropValue =
                    sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
                if (integerCacheHighPropValue != null) {
                    try {
                        int i = parseInt(integerCacheHighPropValue);
                        i = Math.max(i, 127);
                        // Maximum array size is Integer.MAX_VALUE
                        h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                    } catch( NumberFormatException nfe) {
                        // If the property cannot be parsed 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++);
    
                // range [-128, 127] must be interned (JLS7 5.1.7)
                assert IntegerCache.high >= 127;
    
    }
    public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
     }

     

    Integer类与int自动拆箱与装箱

    //拆箱
     public int intValue() {
            return value;
      }
    
    //装箱
    public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
     }
  • 相关阅读:
    092、部署Graylog日志系统(2019-05-16 周四)
    091、万能的数据收集器 Fluentd (2019-05-15 周三)
    090、ELK完成部署和使用 (2019-05-13 周二)
    在CentOS7上无人值守安装Zabbix4.2
    089、初探ELK (2019-05-13 周一)
    34、Scrapy 知识总结
    33、豆瓣图书短评
    32、出任爬虫公司CEO(爬取职友网招聘信息)
    31、当当图书榜单爬虫
    30、吃什么不会胖
  • 原文地址:https://www.cnblogs.com/jj81/p/11509299.html
Copyright © 2011-2022 走看看