4.4 数据类型扩展及面试题讲解
时间:2021-02-06 12:12:00
收藏:0
阅读:0
4.4 数据类型扩展及面试题讲解
整数扩展
- 进制
- 二进制:
0b
- 八进制:
0
- 十进制
- 十六进制:
0x
- 二进制:
public class dataTypeExpansion {
public static void main(String[] args) {
int a = 10;
int b = 010; //八进制
int c = 0x10; //十六进制 0-9 A-F 16
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
输出结果为:
10
8
16
浮点数扩展
- 钱怎么表示?
float
,double
是有限的,离散的,存在舍入误差BigDecimal
来表示,数学工具类
public class dataTypeExpansion {
public static void main(String[] args) {
float d = 0.1f; //0.1
double e = 1.0/10; //0.1
System.out.println(d==e);
float f = 21313131313131313f; //21313131313131313
float g = f + 1; //21313131313131314
System.out.println(f==g);
}
}
输出结果为:
false
true
因此最好完全避免使用浮点数进行比较
字符扩展
-
强制转换
- 所有的字符本质还是数字
- 编码 Unicode 表:80 = P 2 字节
- Excel 有 65536 行,等于 2^16
U0000
UFFFF
-
转义字符
\t
制表符\n
换行
public class dataTypeExpansion {
public static void main(String[] args) {
char h = ‘P‘;
char i = ‘浦‘;
System.out.println(h);
System.out.println((int)h); //强制转换
System.out.println(i);
System.out.println((int)i); //强制转换
char j = ‘\u0061‘;
System.out.println(j);
System.out.println("Hello\tWorld");
System.out.println("Hello\nWorld");
}
}
输出结果为:
P
80
浦
28006
a
Hello World
Hello
World
布尔值扩展
Less is more。
代码要精简易读。
if (flag==true){}
= if (flag){}
public class dataTypeExpansion {
public static void main(String[] args) {
boolean flag = true;
if (flag==true){} //新手
if (flag){} //老手
}
}
评论(0)