写点什么

我看 JAVA 之 基本数据类型与封装类型

用户头像
awen
关注
发布于: 2021 年 02 月 27 日

我看 JAVA 之 基本数据类型与封装类型


注:基于 jdk11

java 提供了 8 中基本数据类型,其中 1 个布尔类型,6 个数字类型,1 个字符类型。同时 jdk 为这 8 种基本数据类型提供了相应的封装类型。


boolean & Boolean


boolean


长度为 1 位

数据范围:只有两个值 true、false

默认值为 false

Boolean


boolean 的封装类型

实现了 Serializable、Comparable 接口

重要的成员变量


public static final Boolean TRUE = new Boolean(true);public static final Boolean FALSE = new Boolean(false);
private final boolean value;
public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean");
复制代码

重要的方法

@HotSpotIntrinsicCandidatepublic boolean booleanValue() {
return value;
}
@HotSpotIntrinsicCandidate
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
复制代码


byte & Byte


byte


长度为 1 个字节,有符号

数据范围:最小值-2^7,最大值 2^7-1 即[-128, 127]共 256 个数字

默认值为 0

Byte


继承了 Number 类, Number 抽象类定义了类型转换的抽象方法如 intValue()longValue()floatValue()等,并实现了序列化接口

实现了 Comparable 接口

重要的成员变量

public static final byte   MIN_VALUE = -128;public static final byte   MAX_VALUE = 127;
@SuppressWarnings("unchecked")
public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
复制代码


重要的方法

@HotSpotIntrinsicCandidatepublic static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
@HotSpotIntrinsicCandidate
public byte byteValue() {
return value;
}
复制代码


缓存机制


private static class ByteCache {    private ByteCache(){}
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
}
复制代码


静态内部类 ByteCache 内部的静态 Byte[]缓存了 Byte 的全部 256 个数据。


int & Integer


int


长度为 4 个字节,有符号

数据范围:最小值-2^31,最大值 2^31-1 即[0x80000000, 0x7fffffff]

默认值为 0

Integer


继承了 Number 类

实现了 Comparable 接口

重要的成员变量


@Native public static final int   MIN_VALUE = 0x80000000;
/**
* A constant holding the maximum value an {@code int} can
* have, 2<sup>31</sup>-1.
*/
@Native public static final int MAX_VALUE = 0x7fffffff;
/**
* The {@code Class} instance representing the primitive type
* {@code int}.
*
* @since 1.1
*/
@SuppressWarnings("unchecked")
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
/**
* All possible chars for representing a number as a String
*/
static final char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
@Native public static final int SIZE = 32; 32位
public static final int BYTES = SIZE / Byte.SIZE; 32/8=4 4个字节
复制代码


重要的方法


@HotSpotIntrinsicCandidate
public static String toString(int i) {
int size = stringSize(i);
if (COMPACT_STRINGS) {
byte[] buf = new byte[size];
getChars(i, size, buf);
return new String(buf, LATIN1);
} else {
byte[] buf = new byte[size * 2];
StringUTF16.getChars(i, size, buf);
return new String(buf, UTF16);
}
}
//使用缓存IntegerCache,使用valueOf在空间和时间上要优于直接使用构造方法
@HotSpotIntrinsicCandidate
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
@HotSpotIntrinsicCandidate
public int intValue() {
return value;
}
复制代码


有意思的方法:


System.out.println(Integer.numberOfLeadingZeros(18));System.out.println(Integer.numberOfTrailingZeros(1000));
System.out.println(Integer.bitCount(1));
System.out.println(Integer.bitCount(2));
System.out.println(Integer.bitCount(4));
System.out.println(Integer.bitCount(12));
复制代码


打印结果如下:

27 整数 18 在二进制表示中首部有 27 个连续的 0

3 整数 1000 在二进制表示中尾部有 3 个连续的 0

1 整数 1 在二进制表示中有 1 位是 1

1 整数 2 在二进制表示中有 1 位是 1

1 整数 4 在二进制表示中有 1 位是 1

2 整数 12 在二进制表示中有 2 位是 1

缓存机制


private static class IntegerCache {
static final int low = -128;
static final int high; //通过设置-XX:AutoBoxCacheMax=<size>,指定high的值,默认缓存范围为[-128,127]
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
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;
}
private IntegerCache() {}
}
复制代码


验证缓存代码示例:


package chapter01;
public class TestInteger {
public static void main(String [] args) {
Integer a = Integer.valueOf(6);
Integer b = new Integer(6);
Integer c = 6;
System.out.println(a==b);
System.out.println(a==c);
System.out.println(c==b);
Integer a1 = Integer.valueOf(600);
Integer b1 = new Integer(600);
Integer c1 = 600;
System.out.println("\n");
System.out.println(a1==b1);
System.out.println(a1==c1);
System.out.println(c1==b1);
}
}
复制代码


打印结果:

false

true

false


false

false

false

建议:

包装类型比较是否相等使用 equals()而非==

long & Long


long


长度为 8 个字节,有符号

数据范围:最小值-2^63,最大值 2^63-1 即[0x8000000000000000L, 0x7fffffffffffffffL]

默认值为 0

Long


类似 integer,读者可以自己去分析

float & Float


float


是单精度、长度为 4 个字节共 32 位、符合 IEEE 754 标准的浮点数

默认值为 0.0f

float 与 double 精度不同,所以在将 float 与 double 强制转换的时候会出现精度丢失的问题

Float


继承了 Number 类, Number 抽象类定义了类型转换的抽象方法如 intValue()longValue()floatValue()等,并实现了序列化接口

实现了 Comparable 接口

重要的成员变量

public static final float POSITIVE_INFINITY = 1.0f / 0.0f; 正无穷
public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;负无穷
public static final float NaN = 0.0f / 0.0f; //不是数字 Not a Number
public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f
public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f
public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f
//最大、最小指数
public static final int MAX_EXPONENT = 127;
public static final int MIN_EXPONENT = -126;
//32位,4个字节
public static final int SIZE = 32;
public static final int BYTES = SIZE / Byte.SIZE;
@SuppressWarnings("unchecked")
public static final Class<Float> TYPE = (Class<Float>) Class.getPrimitiveClass("float");
复制代码


double & Double


double


是双精度、长度为 8 个字节共 64 位、符合 IEEE 754 标准的浮点数

默认值为 0.0d

Double


类似 Float,读者可以自己去分析

short & Short


short


长度为 2 个字节,有符号

数据范围:最小值-2^15,最大值 2^15-1 即[-32768, 32767]

默认值为 0

Short


类似 Integer,读者可以自己去分析

char & Character


char


单个 unicode 字符,长度为 16 位

数据范围:最小值 0,最大值 65535 即[0, 65535]

默认值为""

Character


用户头像

awen

关注

Things happen for a reason. 2019.11.15 加入

还未添加个人简介

评论

发布
暂无评论
我看JAVA 之 基本数据类型与封装类型