数据类型详解
1 基本数据类型
整数类型
浮点类型
布尔类型
根据官方文档描述,boolean类型经过编译之后采用int来定义(所以此时boolean占4字节,32bits)。如果是boolean数组则占1字节(8 bits)。详见下面红色标注。
Although the Java Virtual Machine defines a boolean type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java Virtual Machine int data type.
The Java Virtual Machine does directly support boolean arrays. Its newarray instruction (§newarray) enables creation of boolean arrays. Arrays of type boolean are accessed and modified using the byte array instructions baload and bastore (§baload, §bastore).
In Oracle’s Java Virtual Machine implementation, boolean arrays in the Java programming language are encoded as Java Virtual Machine byte arrays, using 8 bits per boolean element.
The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java Virtual Machine type int, the compilers must use the same encoding.
字符类型
代码演示
数据类型细节
long类型数据需要在最后添加L
float类型数据需要在最后添加F
字符必须使用''表示,字符串必须要使用""
转义字符
转义字符:转变字符原有含义
常见的转义字符
转义字符
注意:在java代码注释中不能出现 \
2 引用数据类型
String表示引用数据类型
在Java中一切用""包裹起来的类型都是String类型