数据类型拓展及面试题
1.整数拓展: 二进制 十进制 八进制0 十六进制0x
int i = 10;
int i2 = 010;//八进制
int i3 = 0x10;//十六进制 0~9 A~F
2.浮点数拓展 银行业务里的钱相关问题
//float 有限 离散 舍入误差 大约 接近但不等于
//最好不要使用浮点数进行比较,会有误差
//使用BigDecimal 数学工具类进行比较
public class Demo1 {
public static void main(String[] args) {
float f = 0.1f; //0.1
double d = 1.0/10; //0.1
System.out.println(f==d); //判断是否相等,false
float d1 = 2313131313131f;
float d2 = d1 + 1;
System.out.println(d1==d2); //true
}
}
3.字符拓展
public class Demo2 {
public static void main(String[] args) {
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1);//强制转换
System.out.println(c2);
System.out.println((int)c2);//强制转换
//所有字符的本质还是数字
//编码 Unicode 表:(97 = a 65 = A) 2字节 0-65536
}
}
4.布尔值拓展
boolean flag = true;
//两个式子一样
if(flag==true){} //新手
if(flag){} //老手