数据类型
- 强类型语言
- 要求变量的使用要严格符合规定, 所有变量都必须先定义后才能使用
- 弱类型语言
- Java的数据类型分为两大类
- 基本类型(primitive type)
- 引用类型(reference type)
字节
Byte
/**
* byte 字节, 1 byte = 8 bits(8位)
* 存储有符号的, 以二进制补码表示的整数
* 最小值-128, 最大值127, 默认值0
* byte 类型用在大型数组中可以显著节约空间, 主要代替小整数, 因为byte 变量占用的空间只有int类型的四分之一
* byte 在二进制文件中使用较多
*/
public class ByteTest {
public static void main(String[] args) {
byte a = (byte) -128;
System.out.println(a); // -128
byte b = (byte) 127;
System.out.println(b); // 127
byte c = (byte) -129;
System.out.println(c); // 127
byte d = (byte) 128;
System.out.println(d); // -128
}
}
整数类型
short
/**
* short, 16位, 2个字节, 有符号的以二进制补码表示的整数
* 范围: (-32768 ~ 32767, -2^15 ~ 2^15-1), 默认值0
*/
int
/**
* int, 32位, 4个字节, 有符号的以二进制补码表示的整数
* 范围: (-2147483648 ~ 2147483647, -2^31 ~ 2^31-1), 默认值0
*/
long
/**
* long, 64位, 8个字节, 有符号的以二进制补码表示的整数
* 范围: (-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807, -2^63 ~ 2^63-1), 默认值0L
*/
整数示例
public class IntegerTest {
public static void main(String[] args) {
short a1 = 32767;
System.out.println(a1);
// short a2 = 32768; error 越界
int b1 = 2147483647;
System.out.println(b1);
// int b2 = 2147483648; error 越界
long c1 = 1000000000000L;
System.out.println(c1);
long c2 = 2147483647; // 隐式做了从int变成long的操作
System.out.println(c2);
long c3 = 2147483648L; // 去掉L将报错
System.out.println(c3);
}
}
面试题及扩展
// 整数扩展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;
int i2 = 010; // 八进制0 当前表示8
int i3 = 0x10;// 十六进制0x 0~9 A-F 当前表示16
浮点数类型
float
/**
* float, 单精度, 32位, 4个字节, 符合IEEE 754标准的浮点数, 默认值0.0f。
* 范围: (1.40129846432481707e-45 ~ 3.40282346638528860e+38), 无论正负
* float 和 double 都不能用来表示很精确的数字。
*/
double
/**
* double, 双精度, 64位, 8个字节, 符合IEEE 754标准的浮点数, 默认值0.0d。
* 范围: (4.94065645841246544e-324d ~ 1.79769313486231570e+308d), 无论正负
* float 和 double 都不能用来表示很精确的数字。
*/
浮点数示例
public class FloatingTest {
public static void main(String[] args) {
float f1 = 1.23f;
// float f2 = 1.23; error, float赋值必须带f
double d1 = 4.56d;
double d2 = 4.56; // double 可以省略末尾d, 因为不加, 默认是double存储
System.out.println(f1); //1.23
System.out.println((double) f1); //转换到double, 输出1.23000001907348631
System.out.println(d1); // 4.56
System.out.println((float) d2); // 4.56
System.out.println(f1 == 1.229999999f); // true
System.out.println(f1 - 1.229999999f); // 0.0
System.out.println(d2 == 4.559999999999999999999d);// true
System.out.println(d2 - 4.559999999999999999999dd);// 0.0
}
}
面试题及扩展
// ===============================================
// 浮点数扩展? 银行业务怎么表示? 金额
// BigDecimal 类
// ===============================================
// 最好避免使用浮点数处理金额!!!!
// 最好避免使用浮点数处理金额!!!!
// 最好避免使用浮点数处理金额!!!!
// float 有限 离散 舍入误差 大约 接近但不等于
例子:
float f = 0.1f; // 0.1
double d = 1.0/10; // 0.1
System.out.println(d1==d2);// false
float d1 = 231333333333333333333f;
float d2 = d1 + 1;
System.out.println(d1==d2);// true
字符类型
char
/**
* char是一个单一的16位 Unicode 字符
* 最小值是 \u0000 (即为0);
* 最大值是 \uffff (即为65,535);
* char 数据类型可以存储任何字符;
*/
字符类型示例
public class CharTest {
public static void main(String[] args) {
char a = 'a';
char b = 97; // 根据ascii码转化为a
char c = '我';
char d = '\u4e00'; // "一"字 \u4e00 -- \u9fa5 两万多汉字
System.out.println(a);// a
System.out.println(b);// a
System.out.println(c);// 我
System.out.println(d);// 一
System.out.println((int)c); // 输出当前字符在ASCII码表中对应的数字
// 科普: 以前Unicode 2字节 0 - 65536 Excel
// U0000 UFFFF
// 转义字符
// \t 制表符
// \n 换行
// ......
// 布尔值扩展
boolean flag = true;
if (flag == true) {} // 新手
if (flag) {} // 老手
// Less is More! 代码要精简易读
}
}