Java基本数据类型
1. 整型
- byte
占1字节
空间,取值范围-2^7~(2^7)-1
,二进制首位为符号位,二进制表示0_000_0000~1_111_1111
,1_000_0000记为-128
。
/**
* A constant holding the minimum value a {@code byte} can
* have, -2^7.
*/
public static final byte MIN_VALUE = -128;
/**
* A constant holding the maximum value a {@code byte} can
* have, (2^7)-1.
*/
public static final byte MAX_VALUE = 127;
- short
占2字节
空间,取值范围-2^15~(2^15)-1
,二进制首位为符号位,二进制表示0_000_0000_0000_0000~1_111_1111_1111_1111
。
/**
* A constant holding the minimum value a {@code short} can
* have, -2^15.
*/
public static final short MIN_VALUE = -32768;
/**
* A constant holding the maximum value a {@code short} can
* have, (2^15)-1.
*/
public static final short MAX_VALUE = 32767;
- int(整数无后缀时默认)
占4字节
空间,取值范围-2^31~2^31-1
,二进制首位为符号位。
/**
* A constant holding the minimum value an {@code int} can
* have, -2^31.
*/
@Native public static final int MIN_VALUE = 0x80000000;
/**
* A constant holding the maximum value an {@code int} can
* have, (2^31)-1.
*/
@Native public static final int MAX_VALUE = 0x7fffffff;
- long
占8字节
空间,取值范围-2^63~(2^63)-1
,二进制首位为符号位。
/**
* A constant holding the minimum value a {@code long} can
* have, -2^63.
*/
@Native public static final long MIN_VALUE = 0x8000000000000000L;
/**
* A constant holding the maximum value a {@code long} can
* have, (2^63)-1.
*/
@Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
2. 浮点型 (Java中浮点数采用的是IEEE 754标准)
- 具体参考Java中float/double取值范围与精度
- float
占4字节
空间,取值范围+/-3.4E+38F(6~7 个有效位)
- double(浮点数无后缀时默认)
占8字节
空间,取值范围+/-1.8E+308 (15 个有效位)
3. char (Java采用的是16位的Unicode字符集)
占2字节
空间,表示范围\u0000~\uffff
/**
* The constant value of this field is the smallest value of type
* {@code char}, {@code '\u005Cu0000'}.
*
* @since 1.0.2
*/
public static final char MIN_VALUE = '\u0000';
/**
* The constant value of this field is the largest value of type
* {@code char}, {@code '\u005CuFFFF'}.
*
* @since 1.0.2
*/
public static final char MAX_VALUE = '\uFFFF';
4. boolean
2.3.4. The Type boolean
Although the Java Virtual Machine defines a type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on values. Instead, expressions in the Java programming language that operate on values are compiled to use values of the Java Virtual Machine data type. booleanbooleanbooleanint
The Java Virtual Machine does directly support arrays. Its newarray instruction (§newarray) enables creation of arrays. Arrays of type are accessed and modified using the array instructions baload and bastore (§baload, §bastore). booleanbooleanbooleanbyte
In Oracle’s Java Virtual Machine implementation, arrays in the Java programming language are encoded as Java Virtual Machine arrays, using 8 bits per element. booleanbyteboolean
The Java Virtual Machine encodes array components using to represent and to represent . Where Java programming language values are mapped by compilers to values of Java Virtual Machine type , the compilers must use the same encoding. boolean1true0falsebooleanint
以上是Oracle发布的《Java虚拟机规范》中关于boolean的解释原文
意思大概是:
- JVM没有提供boolean类型专用的字节指令,而是使用int相关指令来代替。
- 对boolean数组的访问与修改,会共用byte数组的baload和bastore指令。
- 简单来说就是:单boolean值占
4字节
空间,boolean数组则是每个元素占1字节
空间。(boolean实际只需要1位空间)
待续。。。