一、概览

序号 数据类型 ** 默认 取值范围 举例说明 对应的封装类 valueOf缓存
1 boolean(布尔值) 8 false true、false boolean b = true; Boolean TRUE、FALSE
2 byte(字节) 8 0 -2^7 - 2^7-1 byte b = 10; Byte -128~127
3 short(短整数) 16 0 -2^15 - 2^15-1 short s = 10; Short -128~127
4 char(字符) 16 0 - 2^16-1 char c = ‘c’; Character 0~128
5 int(整数) 32 0 -2^31 - 2^31-1 int i = 10; Integer -128~127(可配)
6 float(单精度) 32 0.0 -2^31 - 2^31-1 float f = 10.0f; Float
7 long(长整数) 64 0 -2^63 - 2^63-1 long l = 10l; Long -128~127
8 double(双精度) 64 0.0 -2^63 - 2^63-1 double d = 10.0d; Double

二、深入理解

1.boolean

官方定义:The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its “size” isn’t something that’s precisely defined.
通常在程序中作为标志位,true为真,false为假。理论上是占用内存空间最小的数据类型,使用一位即可存储真和假两种情况,但计算机处理数据的最小单位是1个字节,1个字节等于8位,true也就可以在系统层面表示为0000 0001。但Java代码编译成字节码后会用int类型替代boolean类型,JVM(Java虚拟机)实际运行时执行的是字节码。
Java编译后使用int替代boolean类型的原因:对于当下32位的处理器(CPU)来说,一次处理数据是32位(这里不是指32/64位系统,而是指CPU硬件层面),具有高效存取的特点。
image.png

1.1Boolean

  • Booleanboolean封装类,不属于数字,实现Comparable<Boolean>接口

image.png
Boolean.valueOf()方法,对true和false两个值做缓存。
Boolean.toString()会返回true或false的字符串。
Boolean.hashCode()因为只有true和false两个值,其hashCode也是固定的1231和1237
Boolean.equals()重写了Object的equals方法,实际比较的是源码中的value值。
image.png

2.byte

官方定义:The byte data type is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable’s range is limited can serve as a form of documentation.
byte是一个8位有符号整数,用于表示-128~127的数值,在socket开发过程中,通常需要将各种类型的值转为 byte 类型。

  • byte 类型的数据是8位有符号整数
  • byte 类型的取值范围-128~127
  • 1 byte(字节)等于 8 bits(位)

    2.1Byte

    Byte是byte的包装类型,其源码实现可以参考Boolean。其特别之处在于Byte内部有一个静态类ByteCache,对-128到127的数进行缓存,

  • Bytebyte封装类,继承于抽象类Number,实现Comparable<Byte>接口

  • image.png

当调用Byte.valueOf()方法时,会从缓存中直接返回对象,而不是new一个新的Byte对象。
值得注意的是,只有valueOf()会从缓存中获取对象。
image.png
image.png

3.short

官方定义:The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
short是一个16位有符号整数,用于表示-32,768~32,767 的数值,表示的数值范围大于byte小于int。

3.1Short

  • Shortshort封装类,继承于抽象类Number,实现Comparable<Short>接口

image.png
Short类通过内部类实现了-128~127的对象缓存。
image.png

4.char

官方定义:The char data type is a single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).
char是一个16位无符号正整数,取值范围是0~65535。它的特点是没有负数,且可转化成字符。
Unicode官网:https://home.unicode.org/
ASCII码表

4.1Character

  • Characterchar的封装类,实现Serializible、Comparable<Character>接口

image.png
Character类实现了可序列化接口并声明了序列化ID(serialVersionUID),其作用是在序列化时验证版本一致性。
Character类同样通过内部类实现了对象缓存,但因为char是非负数,所以只有0~128的范围缓存。
image.png
Character类的equals方法同样是比较的value值。
而他的toString方法则是将value值转换为char数组格式,通过String.valueOf()转换成字符。
image.png

5.int

官方定义:By default, the int data type is a 32-bit signed two’s complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.
int是一个32位有符号整数类型,取值范围是-2^31 - 2^31-1,是最常用的基础数据类型。

5.1Integer

  • Integerint的封装类,继承于抽象类Number,实现Comparable<Integer>接口

image.png
Integer内部同样存在内部类实现对象缓存,需要注意的是low不可变,而high可变。
Integer的缓存范围可以通过设置JVM参数-Djava.lang.Integer.IntegerCache.high=数字的方式设置缓存范围。
image.png
image.png

6.float

官方定义:The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.
float是一个32位单精度有符号数字,取值范围是-2^31 - 2^31-1。

6.1Float

  • Floatfloat的封装类,继承于抽象类Number,实现Comparable<Float>接口

image.png
Float不存在缓存
image.png

7.long

官方定义:The long data type is a 64-bit two’s complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.
long是一个64位有符号整数,取值范围是-2^63 - 2^63-1。

7.1Long

  • Longlong封装类,继承于抽象类Number,实现Comparable<Long>接口

image.png
Long实现了内部类缓存,范围是-128~127。
image.png
Long.toString()方法对value进行转换char[],然后创建为一个String对象返回。
image.png

8.double

官方定义:The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
double是一个64位有符号双精度数,取值范围是-2^63 - 2^63-1。

8.1Double

  • Doubledouble封装类,继承于抽象类Number,实现Comparable<Double>接口

image.png
Double不存在缓存。
image.png

参考文献

Oracle官方文档对数据类型的说明
Oracle官网Java虚拟机指令集