数据类型扩展

数据类型扩展

整数拓展

进制转换:

二进制 0b 八进制0 十六进制0x

 int i = 10;
 int i2 = 010;//八进制0
 int i3 = 0x10;//十六进制0x       0~9   A~F
 int i4 = 0b10;

 System.out.println(i);//10
 System.out.println(i2);//1*16^1+0*16^0=16
 System.out.println(i3);//1*8^1+0*8^0=8
 System.out.println(i4);//1*2^1+0*2^0=2

浮点数拓展

银行业务怎么表示? 会用BigDecimal 数学工具类
因为float类型是有限的,离散的,有舍入误差是大约的是接近但不等于的所以最好完全避免使用浮点数进行比较

最好完全避免使用浮点数进行比较

最好完全避免使用浮点数进行比较

最好完全避免使用浮点数进行比较

 float f  = 0.1f;
 double d = 1.0/10;

 System.out.println(f==d);//false
 System.out.println(f);//0.1
 System.out.println(d);//0.1

 float d1 = 101011010101010f;
 float d2 = d1+1;
 System.out.println(d1==d2);//true

通过以上测试表明float 和 double这两种类型有误差所以不能使用

字符拓展

 char c1 = 'a';
 char c2 = '中';

 System.out.println(c1);//a
 System.out.println((int)c1);//强制转换   97

 System.out.println(c2);//中
 System.out.println((int)c2);//20013

这表明所有字符本质还是数字
编码用的是 Unicode 2字节 0-65536 有人测试过Excel是 2^16=65536

Unicode的编码 U0000~UFFFF

char c3 = '\u0061';
 System.out.println(c3);//a

转义字符有 \t 制表符 、\n 换行符等。。。。。

 System.out.println("Hello\n");//Hello+换行
 System.out.println("Hello\tWorld");//Hello    World
 String sa = new String("hello");
 String sb = new String("hello");

 String sc ="hello";
 String sd ="hello";

 System.out.println(sa);//hello
 System.out.println(sb);//hello

 System.out.println(sa==sb);//false
 System.out.println(sc==sd);//true

此处涉及到面向对象sa和sb都是输出hello但它们是不同的对象

布尔值拓展

 boolean flag = true;
 if(flag==true){}//不够简洁
 if (flag){}//简洁

以上两种表达都是一样的
但代码要精简易读

posted @ 2022-02-24 00:13  tantao0_0  阅读(32)  评论(0)    收藏  举报