Skip to content

Commit 41eaf4b

Browse files
authored
Merge pull request CyC2018#695 from jsycdut/patch-1
完善Java包装类型的缓冲池的范围,补充Integer缓冲池的特殊之处
2 parents eccb382 + b106daa commit 41eaf4b

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

notes/Java 基础.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,22 @@ System.out.println(m == n); // true
137137

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

140-
- boolean values true and false
141-
- all byte values
142-
- short values between -128 and 127
143-
- int values between -128 and 127
144-
- char in the range \u0000 to \u007F
140+
|基本类型|包装类型|包装类型缓冲池|缓冲池内容|
141+
|:-:|:-:|:-:|:-:|
142+
|boolean|Boolean|||
143+
|byte|Byte|ByteCache|Byte类型数据,数值范围[-128, 127]|
144+
|char|Character|CharacterCache|Character类型数据,数值范围[0, 127],对应字符内容为整个ASCII码表|
145+
|short|Short|ShortCache|Short类型数据,数值范围[-128, 127]|
146+
|int|Integer|IntegerCache|Integer类型数据,数值范围默认[-128, 127],最大值可调|
147+
|long|Long|LongCache|Long类型数据,数值范围[-128, 127]|
148+
|float|Float|||
149+
|double|Double|||
145150

146-
在使用这些基本类型对应的包装类型时,就可以直接使用缓冲池中的对象。
151+
在使用这些基本类型对应的包装类型时,如果该数值范围在缓冲池范围内,就可以直接使用缓冲池中的对象。
152+
153+
**Integer缓冲池的特殊性**
154+
155+
在jdk1.8所有的数值类缓冲池中,Integer的缓冲池IntegerCache很特殊,这个缓冲池的下界是-128,上界默认是127,但是这个上界是可调的,在启动jvm的时候,通过`-XX:AutoBoxCacheMax=<size>`来指定这个缓冲池的大小,该选项在JVM初始化的时候会设定一个名为`java.lang.IntegerCache.high`系统属性,然后IntegerCache初始化的时候就会读取该系统属性来决定上界。
147156

148157
[StackOverflow : Differences between new Integer(123), Integer.valueOf(123) and just 123
149158
](https://stackoverflow.com/questions/9030817/differences-between-new-integer123-integer-valueof123-and-just-123)
@@ -152,7 +161,7 @@ System.out.println(m == n); // true
152161

153162
## 概览
154163

155-
String 被声明为 final,因此它不可被继承。
164+
String 被声明为 final,因此它不可被继承。(注意,所有8种基本类型对应的包装类型全都是由`final`修饰的不可变类)
156165

157166
在 Java 8 中,String 内部使用 char 数组存储数据。
158167

0 commit comments

Comments
 (0)