我看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; }
- 緩存機制
靜态内部類ByteCache内部的靜态Byte[]緩存了Byte的全部256個資料。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)); } }
int & Integer
- int
- 長度為4個位元組,有符号
- 資料範圍:最小值-2^31,最大值2^31-1 即[0x80000000, 0x7fffffff]
- Integer
- 繼承了Number類
-
@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]
- Long
類似 integer,讀者可以自己去分析
float & Float
- float
- 是單精度、長度為4個位元組共32位、符合 IEEE 754 标準的浮點數
- 預設值為0.0f
- float與double精度不同,是以在将float與double強制轉換的時候會出現精度丢失的問題
- Float
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]
- Short
類似 Integer,讀者可以自己去分析
char & Character
- char
- 單個unicode字元,長度為16位
- 資料範圍:最小值0,最大值65535 即[0, 65535]
- 預設值為""
- Character