Note: Based on jdk11
java provides 8 basic data types, including 1 Boolean type, 6 numeric types, and 1 character type.jdk also provides corresponding encapsulation types for the eight basic data types.
boolean & Boolean
-
boolean
- 1 bit in length
- Data Range: Only two values true, false
- Default value is false
-
Boolean
- Encapsulation type of boolean
- Serializable, Comparable interfaces implemented
-
Important member variables
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");
-
Important methods
@HotSpotIntrinsicCandidate public boolean booleanValue() { return value; } @HotSpotIntrinsicCandidate public static Boolean valueOf(boolean b) { return (b ? TRUE : FALSE); }
byte & Byte
-
byte
- 1 byte long, signed
- Data Range: Minimum -2^7, Maximum 2^7-1 [-128, 127] 256 digits
- Default value is 0
-
Byte
- Inherited the Number class, which defines abstract methods for type conversion such as intValue()longValue()floatValue() and implements a serialization interface
- Implement Comparable interface
-
Important member variables
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");
-
Important methods
@HotSpotIntrinsicCandidate public static Byte valueOf(byte b) { final int offset = 128; return ByteCache.cache[(int)b + offset]; } @HotSpotIntrinsicCandidate public byte byteValue() { return value; }
-
Caching mechanism
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)); } }
The static Byte[] inside the static internal class ByteCache caches all 256 data of Byte.
int & Integer
-
int
- 4 bytes long, signed
- Data Range: Minimum -2^31, Maximum 2^31-1 [0x80000000, 0x7fffffff]
- Default value is 0
-
Integer
- Inherited Number class
- Implement Comparable interface
-
Important member variables
@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 position public static final int BYTES = SIZE / Byte.SIZE; 32/8=4 4 Bytes
-
Important methods
@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); } } //Using cached IntegerCache, using valueOf is better in space and time than using the construction method directly @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; }
-
Interesting methods:
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)); //The printout is as follows: 27 Integer 18 starts with 27 consecutive zeros in a binary representation 3 The integer 1000 has three consecutive zeros at the end of the binary representation 1 Integer 1 has 1 bit in a binary representation and is 1 1 The integer 2 has 1 bit in the binary representation and is 1 1 One bit of integer 4 in the binary representation is 1 2 The integer 12 has 2 bits 1 in the binary representation
-
Caching mechanism
private static class IntegerCache { static final int low = -128; static final int high; //Specify the value of high by setting -XX:AutoBoxCacheMax=<size>, and the default cache range is [-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() {} }
Example validation cache code:
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); } } //Print results: false true false false false false //Suggestions: //Compare packaging types using equals() instead of ==
long & Long
-
long
- 8 bytes long, signed
- Data Range: Minimum -2^63, Maximum 2^63-1 is [0x8000000000000L, 0x7fffffffffffffL]
- Default value is 0
-
Long
Like integer, readers can analyze for themselves
float & Float
-
float
- Is single precision, 4 bytes in length, 32 bits in total, consistent IEEE 754 Standard floating point number
- Default value is 0.0f
- Float is not the same precision as double, so precision loss can occur when casting float to double
-
Float
- Inherited the Number class, which defines abstract methods for type conversion such as intValue()longValue()floatValue() and implements a serialization interface
- Implement Comparable interface
- Important member variables
public static final float POSITIVE_INFINITY = 1.0f / 0.0f; plus infinity public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;negative infinity 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 //Maximum and Minimum Exponents public static final int MAX_EXPONENT = 127; public static final int MIN_EXPONENT = -126; //32-bit, 4 bytes 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
- Is a double-precision, 8-byte 64-bit floating point number compliant with the IEEE 754 standard
- Default value is 0.0d
-
Double
Like Float, readers can analyze it themselves
short & Short
-
short
- 2 bytes long, signed
- Data Range: Minimum -2^15, Maximum 2^15-1 [-32768, 32767]
- Default value is 0
-
Short
Like Integer, readers can analyze for themselves
char & Character
-
char
- Single unicode character, 16 bits long
- Data Range: Minimum 0, Maximum 65535 [0, 65535]
- Default value is ""
- Character