数据类型拓展
整数拓展
public class Demo04 {
public static void main(String[] args) {
int i = 10;
int i2 = 010;//八进制
int i3 = 0x10;//十六进制
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
}
}
浮点数拓展
-
BigDecimal : 大数据类
-
**float : 有限 离散 舍入误差 大约 接近但不等于 **
-
double
- 最好完全避免使用浮点数进行比较!
- 最好完全避免使用浮点数进行比较!
- 最好完全避免使用浮点数进行比较!
字符串拓展
public class Demo03 {
public static void main(String[] args) {
char c1 = 'A';
char c2 = '哎';
System.out.println(c1);
System.out.println(c2);
/*
所有的字符本质是数字
编码 Unicode 2字节 0 - 65536
65 = A
U0000 - UFFFF
*/
System.out.println((int)c1);
System.out.println((int)c2);
char c3 = '\u0061';
System.out.println(c3);
}
}
转义字符
布尔值扩展
public class Demo04 {
public static void main(String[] args) {
boolean flag = true;
//默认是 true
if(flag){
System.out.println("你好!");
}else {
System.out.println("不好! ");
}
}
}
类型转换的方法及注意点
public class Demo05 {
public static void main(String[] args) {
int i = 128;
byte b = (byte) i;
//强制转换 高 → 低 (类型)变量名
//自动转换 低 → 高
System.out.println(i);
System.out.println(b);
/*
注意点:
1. 不能对 boolean 进行转换 (boolean 是位运算)
2. 不能把对象类型转换为不相干的类型
3. 在把大容量转换成低容量的时候进行强制转换
4. 转换的时候可能存在内存益处或者精度问题
*/
System.out.println("=======================");
System.out.println((int)3.14); // 3
System.out.println((int)-3.14f); // -3
System.out.println("=======================");
char c = 'A';
int d = c+1;
System.out.println(d);
System.out.println((char)d);
}
}
对于大数操作的类型转换
public class Demo06 {
public static void main(String[] args) {
//操作比较大的数的时候,注意内存溢出。
//JDK7新特性,数字之间可以用下划线分割。
int money = 10_0000_0000;
System.out.println(money);
int years = 20;
int total1 = money*years;//-1474836480 内存溢出
long total2 = money*years;//-1474836480 内存溢出
System.out.println(total1);
System.out.println(total2);
long total3 = money*(long)years;
System.out.println(total3); // 200_0000_0000
}
}