数据类型及拓展
位和字节
- 
位:bit 计算机中最基本的单位 表示为1或者0。 
- 
字节:byte一字节=8位 1byte=8bibt 1byte 存1个英文字母,2个byte存一个汉字 1kb=1024byte 其他以此类推 
Java八大基本数据类型及拓展
基本数据类型
- 
boolean:只占1位,且只有true和false两个取值。 
- 
byte:1字节 (8位),最大存储数据量是255,存放的数据范围是-128~127之间。 
- 
short:2字节(16位),最大数据存储量是65536,数据范围是-32768~32767之间。 
- 
char:2字节(16位),存储Unicode码,用单引号赋值。 
- 
int:4字节(32位),最大数据存储容量是2的32次方-1,数据范围是负的2的31次方到正的2的31次方-1。 
- 
float:4字节(32位),数据范围在3.4e-45~1.4e38,直接赋值时必须在数字后加上f或F。 
- 
long:8字节(64位),最大数据存储容量是2的64次方-1,数据范围为负的2的63次方到正的2的63次方-1。 
- 
double:8字节(64位),数据范围在4.9e-324~1.8e308,赋值时可以加d或D也可以不加。 
整数拓展
进制表示:
- 
二进制:0b 
- 
八进制:0 
- 
十六进制:0x 
浮点数拓展
1 float f1=23333333333333323232f; 2 float f2=f1+1; 3 System.out.println(f1==f2); 4 //结果位true
1 float f=0.1f; 2 double d=1.0/10; 3 System.out.println(f==d); 4 //结果为flase
综上浮点数是离散、有限的,在比较的时候很容易出现误差
所以最好完全避免使用浮点数进行比较
字符拓展
1 String s1=new String("hello world"); 2 String s2=new String("hello world"); 3 System.out.println(s1==s2); 4 //结果为flase 5 String s3="hello world"; 6 String s4="hello world"; 7 System.out.println(s3==s4); 8 //结果为true
- 
字符的本质还是数字 
数据类型转换
低------------------------------------------------------------------>高 byte,short,char--> int --> long--> float --> double
- 
强制类型转换:从高到低时(括号里写要转换的类型) 如 double d=123.45; System.out.println((int)d); 
- 
自动类型转换:由低到高时(无需转换 自动变) 如 int n1=123; double n2=n1; System.out.println(n2); //结果为123.0 
- 
不能对布尔值进行转换 
- 
不能把对象类型转换为毫不相干的类型 
- 
由高到低转换时可能会出现内存溢出或者精度问题 int n1=128; System.out.println((byte)n1); //输出为-128 float n2=123.45f; System.out.println((int)n2); //输出为123 
本文来自博客园,作者:紫英626,转载请注明原文链接:https://www.cnblogs.com/recorderM/p/14383790.html

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号