005 java数据类型

数据类型

强类型语言和弱类型语言

强类型:要求变量使用严格符合规定,所有变量先定义再使用

变量就是可以变化的量,定义完用分号结尾

基本类型和引用类型


基本类型:8大类型

生活中用到数字(整数、小数)、字符、是非、

整数:byte 1字节 int 4字节 short 2字节 long 8字节

浮点数:float 4字节 double 8字节

字符:char 2字节

布尔类型:boolean 1位

需要注意的是:

浮点数默认用的是double,如果要用float类型需要在数字后面加上F;

长整形和短整型默认用的是short,如果要用long类型需要在数字后面加上L;

输入完整关键字可以按住ctrl单击看关键字的解释,主要是可以看此种数据类型的取值范围

char是单个字符,用单引号

String是类,不是关键字,用双引号


引用类型:除了基本的数据类型以外就是引用类型

  • 接口
  • 数组

整数拓展

不同进制的表示

二进制前面加上 0b

八进制前面加上 0

十六进制前面加 0x (0-9 A-F)

int i = 10;//十进制表示10
int i1 = 010;//八进制表示8
int i2 = 0b10;//二进制表示2
int i3 = 0x1f;//十六进制表示(16+15=31),0-9 A-F

浮点数拓展

float f = 0.1F;
double d = 1.0/10;
System.out.println(f==d);//虽然都等于0.1,但是判断f和d并不相等
System.out.println(f);
System.out.println(d);
float d1 = 121212145454545452f;
float d2 = d1 +1;
System.out.println(d1);
System.out.println(d2);
System.out.println(d1==d2);//尽管d1和d2不相等,但是由于d1位数太大,实际上判断是相等

浮点数表示的数是有有限的,也是离散的,存在舍入误差,表示的是大约数,接近但不等于,不能用于比较和精确计算
最好完全避免使用浮点数进行比较
银行业务会使用一个类来表示,BigDecimal

字符拓展

        char c1 = 'A';
        char c2 = '中';
        System.out.println(c1);//输出为A
        System.out.println((int)c1);//强制转换,输出65
        System.out.println(c2);//输出为中
        System.out.println((int)c2);//强制转换,输出为20013
		//所有的字符本质都是数字,是由于采用的是UNICODE编码系统,把各种文字都以2字节编码,0~65535(2 sup16=65536)
        System.out.println(c1==65);//输出为true
        System.out.println(c2==20013);//输出为true
        char c3 = 65;//
        char c4 = 20013;//
        System.out.println(c3);//输出为A
        System.out.println(c4);//输出为中
        char c5 = '\u0061';// 斜杠u代表转义,0061是十六进制表示的97
        System.out.println(c5);//输出为a
        System.out.println((int)c5);//输出为97
        char c6 = 97;
        System.out.println(c6);//输出为a
//转义字符拓展
        System.out.println("abc \n abc");//n代表回车
        System.out.println("abc \t abc");//t代表table水平制表符
//字符串拓展
        String sa = new String("hello world");
        String sb = new String("hello world");
        System.out.println(sa==sb);

        String sc = "hello world";
        String sd = "hello world";
        System.out.println(sc==sd);//主要是内存地址不同

        System.out.println("======================");
        
//布尔值拓展
        boolean flag = true;
        if (flag==true){}
        if (flag){}//以上两行都是一样的,代码要精简易读
posted @ 2021-02-16 19:42  ytytytyt  阅读(39)  评论(0)    收藏  举报