数据类型
- 基本类型(四类八种)
- byte
- char
- short (-128~127)
- int
- float
- long
- double
- boolean
- 包装类型
Integer a = 1;//使用了自动装箱,调用了Integer.valueOf(1)
int b = a;//使用了自动拆箱,调用了 a.intValue()
- 缓存池
Integer a = new Integer(2); //每次都会新建一个对象
Intger b = Integer.valueOf(2); //使用缓存池种的对象,多次调用使用的是同一个对象的引用
Integer c = new Integer(2);
Intger d = Integer.valueOf(2);
Integer e = Integer.valueOf(128);
Integer f = Integer.valueOf(128);
System.out.println(a == c);// false
System.out.println(b == d);// true
System.out.println(e == f);// false
缓存池种 Integer 的下限是 -128 ,上限默认127 。JDK1.8后可调
String
- 概述
- String,字符串,在开发中使用频率非常高。因为使用 final 修饰,所以不能被继承。在 Java 8 中,String底层使用 char 数组存储数据
因为 value 数组被声明为 final ,且内部没有改变 value 的方法,因此String不可变。private final char value[];
这个特性使得 String 类型在日常开发中经常被用作 HashMap 的 key。
因为不可变,所以 String 线程安全 - StringBuffer,StringBuilder
- StringBuffer 和 StringBuilder 都继承抽象类 AbstractStringBuilder,可变但不能被继承
- StringBuffer 线程安全,StringBuilder 线程不安全。
- 涉及频繁的字符串拼接时考虑使用这个两个类,因为每次对String 类进行改变的时候,都会生成一个新的 String 对象。