我看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");
-
重要的方法
@HotSpotIntrinsicCandidate public 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");
-
重要的方法
@HotSpotIntrinsicCandidate public 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