Update Java 基础.md

This commit is contained in:
BrkingYan 2019-08-08 19:45:05 +08:00 committed by GitHub
parent d2a04c48b9
commit b118cc4b53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -63,8 +63,8 @@ boolean 只有两个值true、false可以使用 1 bit 来存储,但是
基本类型都有对应的包装类型基本类型与其对应的包装类型之间的赋值使用自动装箱与拆箱完成 基本类型都有对应的包装类型基本类型与其对应的包装类型之间的赋值使用自动装箱与拆箱完成
```java ```java
Integer x = 2; // 装箱 Integer x = 2; // 装箱 调用了 Integer.valueOf(2);
int y = x; // 拆箱 int y = x; // 拆箱 调用了 Integer.intValue(x);
``` ```
## 缓存池 ## 缓存池
@ -78,6 +78,11 @@ new Integer(123) 与 Integer.valueOf(123) 的区别在于:
Integer x = new Integer(123); Integer x = new Integer(123);
Integer y = new Integer(123); Integer y = new Integer(123);
System.out.println(x == y); // false System.out.println(x == y); // false
Integer x = 123; //调用了Integer.valueOf(123);
Integer y = 123; //如果数值在[-128,127]之间便返回指向缓冲池中已经存在的对象的引用否则创建一个新的Integer对象
System.out.println(x==y); //true
Integer z = Integer.valueOf(123); Integer z = Integer.valueOf(123);
Integer k = Integer.valueOf(123); Integer k = Integer.valueOf(123);
System.out.println(z == k); // true System.out.println(z == k); // true
@ -154,7 +159,7 @@ System.out.println(m == n); // true
## 概览 ## 概览
String 被声明为 final因此它不可被继承 String 被声明为 final因此它不可被继承(Integer等包装类也不能被继承)
Java 8 String 内部使用 char 数组存储数据 Java 8 String 内部使用 char 数组存储数据