数据类型之Integer与int

数据类型之Integer与int

基本数据类型

众所周知,Java是面向对象的语言,一切皆对象。但是为了兼容人类根深蒂固的数据处理习惯,加快常规数据的处理速度,提供了9种基本数据类型,他们都不具备对象的特性,没有属性和行为。Java的9种基本数据类型包括 boolean、byte、char、short、int、long、double和refvar。前8种数据类型表示生活中的真假、字符、整数和小数,最后一种refvar是面向对象世界中的引用变量,也叫引用句柄。此处认为它也是一种基本数据类型。
前8种都有其对应的包装数据类型,除了char的对应包装类名为 Character, int为 Integer外,其它所有对应的包装类名就是把首字母大写即可。

下面列出了这8种基本数据类型的空间占用大小(bit)及对应的包装类等信息。

  • boolean/1/Boolean
    • byte/8/Byte
    • char/16/Character
    • short/16/Short
    • int/32/Integer
    • float/32/Float
    • long/64/Long
    • double/64/Double

包装类型

前8种基本数据类型都有相应的包装类,因为Java的设计理念是一切皆是对象,在很多情况下,需要以对象的形式操作,比如hashCode()获取哈希值,或者getClass获取类等。包装类的存在解决了基本数据类型无法做到的事情:泛型类型参数、序列化、类型转换、高频区间数据缓存。
重点要说的就是最后一项:缓存池

缓存池

事实上除了Float和Double外,其他包装类型都会缓存。接下来我以介绍Integer的方式来讲解缓存。
先看一段代码

1
2
new Integer(123);
Integer.valueOf(123);

new Integer(123) 与 Integer.valueOf(123) 的区别在于:

  • new Integer(123) 每次都会新建一个对象
  • Integer.valueOf(123) 会使用缓存池中的对象,多次调用会取得同一个对象的引用。

valueOf() 方法的源码。

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

如上源代码,赋值数据i在缓存区间内直接返回缓存池中的Integer对象,否则就会new一个对象。
因此比较时会出现如下问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class TestIntegerCache {
public static void main(String[] args) {
Integer i1 = 100;
Integer i2 = 100;
System.out.println(i1 == i2);
//true

Integer i3 = 500;
Integer i4 = 500;
System.out.println(i3 == i4);
//false
System.out.println(i3.equals(i4));
//true
}
}

所以推荐所有包装类型对象之间的比较,全部使用equals方法。

在 Java 8 中,Integer 缓存池的大小默认为 -128~127。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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;
}

基本类型对应的缓冲池如下:

  • boolean values true and false
  • all byte values
  • short values between -128 and 127
  • int values between -128 and 127
  • char in the range u0000 to u007F

相关参考:
StackOverflow : Differences between new Integer(123), Integer.valueOf(123) and just 123

本文标题:数据类型之Integer与int

文章作者:谢嘉威

发布时间:2018年11月01日 - 20:11

最后更新:2018年11月08日 - 19:11

原始链接:https://www.xiejiawei.cc/2018/11/01/数据类型之Integer与int/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

捐助作者
原文地址:https://www.cnblogs.com/jiaweixie/p/9931226.html