0
点赞
收藏
分享

微信扫一扫

1.2 Java基础 数据类型(Integer源码解析)



Java数据类型和Integer源码解析

  • 1.2.1 基本数据类型
  • 1.2.2 什么是拆装箱
  • 1.2.3 拆装箱是如何实现的
  • 1.2.4 Integer继承关系
  • 1.2.5 Integer源码解析




1.2.1 基本数据类型

       如大家所知,Java是一门面向对象的语言,但是java并非完全面向对象,Java中的数据类型分为了
       原型(Primitive data types)和非原型(Non-primitive data types),所有的类,接口数组,包装类都是非原型,或者可以说除了八种原型之外的数据类型都是非原型。

原型

原型

概述

范围

默认值

boolean

true或者false

1或者0

false

byte

8位整型

-128 - 127

0

char

char即16位的Unicode编码字符

‘\u0000’ - ‘\uffff’

‘\u000’

short

16位整型

-32768 - 32767

0

int

32位整型

-2^31 - 2^31-1

0

long

64位整型

-2^63 - 2^63-1

0

float

32位浮点数

0.0f

double

64位浮点数

0.0d


1.2.2 什么是拆装箱

       以Integer 为例:
       正常我们定义一个int变量如
       int num = 1,但是我们用Integer时可以直接写
       Integer num = 1,这就是装箱

       而当我们有一个Integer num = 1;
       int var = num;这样的用法时,我们直接将一个Integer的值赋给了一个int变量,这就是拆箱

1.2.3 拆装箱是如何实现的

       在Integer中定义了一个value的int型属性。

final static int value;

       这个值是真正用的值,拆箱就是返回该value值,装箱就是调用valueOf()重新得到一个

       java Integer装箱int值源码

public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}


1.2.4 Integer继承关系

1.2 Java基础 数据类型(Integer源码解析)_整型


       首先可以看到,Integer继承了抽象类Number,Number类的作用是对数值类的一种规范,表示其可以转化成其他原型如long,double,short… 该抽象类定义了一系列转化成原型的抽象方法,其具体实现在其继承类里,故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();
    }
    private static final long serialVersionUID = -8742448824652078965L;
}


1.2.5 Integer源码解析

       由上述代码可知,在某一个范围内,返回的是一个cache的值,不在这个范围内则重新new一个Integer。

       问题来了IntegerCache.cache是什么?
       看源码如下:

private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];

    static {
        // high value may be configured by property
        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;
    }

    private IntegerCache() {}
}


揭晓答案:

cache = new Integer[(high - low) + 1];

        里边就已经放了一些Integer值了,上述代码在low和high这个范围里取到的就是这个数组的值。

        这个数组有何特点呢?
        由代码知道数组第一位放了low值也就是
        cache[0] = -128;
        cache[1] = -127;
        …依次类推

       这样我们就知道取值的时候如果i = 1;cache[1 + 128] 是多少呢,当然就是1啦!


举报

相关推荐

0 条评论