4.数据类型扩展及面试题讲解
这章开始可以运行代码了。
·右键运行(推荐)
·或者上面菜单栏点运行的图标(有时候运行的不是本文件)
//整数拓展:进制的头部 二进制'0b'开头 十进制 八进制0 十六进制0x
int i=10;//十进制
int i2=010;//八进制0
int i3=0x10;//十六进制0x 0~9 A~F 16
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println("==================");
浮点数拓展
最好 完全避免使用 浮点数进行比较
最好 完全避免使用 浮点数进行比较
最好 完全避免使用 浮点数进行比较
//float和double是有问题的
double d=1.0/10;
float f=0.1f;
System.out.println(d);//0.1
System.out.println(f);//0.1
System.out.println(d==f);//false
如何使用debug调试
float q=213124124f;
float w=q+1;
System.out.println(q==w);//true
//浮点的字长是有限的,离散的,存在舍入误差,结果只能是一个大约数,接近不等于
//最好完全避免使用浮点数进行比较
//浮点数拓展:银行业务怎么办?
//BigDecimal数学工具类
字符拓展-强制转换
所有的字符本质还是数字
编码 Unicode表:(a=97 A=65) 2字节 最多可以表示65536(现在有更多了)
char c1='a';
char c2='中';
System.out.println(c1);
//把字符变成数字
System.out.println((int)c1);//强制转换,输出97
System.out.println(c2);
System.out.println((int)c2);//强制转换,输出20013
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
通过Unicode编码转译表示,U0000~UFFFF
char c3='\u0061'
System.out.println(c3);//a
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
转义字符
转译字符
/t 制表符
/n 换行
...
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
等以后学到对象(这一课时)再从内存分析
String sa = new String( original: "hello world");
String sb = new String( original: "hello world");
System . out . println(sa==sb);//输出false
String SC = "he1lo world"; .
String sd = "hello world"; .
System. out . println(sc==sd);//输出true
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
布尔值扩展
boolean flag = true;
if (flag==true){} //新手
if (flag){} //老手
//代码要精简易读