说到Java中的包装类就不得不介绍一下Java中的基本数据类型(8种):byte、short、int、long、float、double、char、boolean。下面以表格的形式详细介绍这八种数据类型:
| byte | short | int | long | float | double | char | boolean | |
| 位数 | 8 | 16 | 32 | 64 | 32 | 64 | 16 | 1 |
| 字节数 | 1 | 2 | 4 | 8 | 4 | 8 | 2 | 1(1/8) |
| 默认值 | 0 | 0 | 0 | 0L | 0.0f | 0.0d | false | |
| 包装类型 | Byte | Short | Integer | Long | Float | Double | Character | Boolean |
特别说明:
- char类型占2个字节,可以表示汉字,所以无论是汉字还是英文字符都占2字节
- boolean类型理论上占1bit(1/8字节),而实际中按1byte(1字节)处理
- 基本数据类型之间的转换:低级向高级自动转换(隐式),高级向低级强制转换(显式)
1 public class DataTypeDemo { 2 3 byte a; 4 short b; 5 int c; 6 long d; 7 float e; 8 double f; 9 boolean g; 10 char h; 11 12 public static void main(String[] args) { 13 /* 14 * 几种基本数据类型的大小(bit) 15 */ 16 System.out.println(Byte.SIZE); //8 17 System.out.println(Short.SIZE);//16 18 System.out.println(Integer.SIZE);//32 19 System.out.println(Long.SIZE);//64 20 System.out.println(Float.SIZE);//32 21 System.out.println(Double.SIZE);//64 22 System.out.println(Character.SIZE);//16 23 24 /* 25 * 基本数据类型的默认值 26 */ 27 DataTypeDemo dtd = new DataTypeDemo(); 28 System.out.println(dtd.a);//0 29 System.out.println(dtd.b);//0 30 System.out.println(dtd.c);//0 31 System.out.println(dtd.d);//0 32 System.out.println(dtd.e);//0.0 33 System.out.println(dtd.f);//0.0 34 System.out.println(dtd.g);//false 35 System.out.println(dtd.h);// 36 37 /* 38 * char类型可以表示汉字 39 */ 40 char ch = '犇'; 41 System.out.println(ch);//犇 42 } 43 44 }