Java数据
1.八大数据类型
整数
- byte 范围:-128~127
- short 范围:-32768~32767
- int 范围:-2147483648~2147483647
- long 范围:-922337203685475808~9223372036854775807
byte num1 = 14;
short num2 = 30;
int num3 = 1234567890; //最常用
long num4 = 30L; //Long类型要在数字后面加L
小数(浮点数)
- float
- double
float num5 = 1.5F; //float类型要在后面加F
double num6 = 3.1415926;
字符
- char
char name = 'A';
布尔值
- boolean
//布尔值:是非
boolean flag = true;
boolean flag1 = false;
字符串
- String
==String不是关键字,是类==
2.数据类型拓展
2.1整数拓展
- 二进制 0b
- 八进制 0
- 十进制
- 十六进制 0x
代码
int i = 10;
int i2 = 010;//八进制0
int i3 = 0x10;//十六进制 0~9 A~F 16
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
运行结果
10
8
16
2.2浮点数拓展
代码
//浮点数拓展 银行业务怎么表示?钱
//float 有限 离散 舍入误差 大约 接近但不等于
//double
//BigDecimal 数学工具类
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
float f = 0.1f;//0.1
double d = 1.0 / 10;//0.1
System.out.println(f == d);//false
float d1 = 2154545454f;
float d2 = d1 + 1;
System.out.println(d1 == d2);//true
运行结果
false
true
2.3字符拓展
代码
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int) c1);//强制类型转换
System.out.println(c2);
System.out.println((int) c2);//强制类型转换
//所有的字符本质还是数字
//编码 Unicode 2字节 65536 Excel 2 12 = 65536
// U0000 UFFFF
char c3 = '\u0061';
System.out.println(c3);
运行结果
a
97
中
20013
a
2.4转义字符
代码
// \t 制表符
// \n 换行
// ......
System.out.println("hello\nworld");
String sa = new String("hello");
String sb = new String("hello");
System.out.println(sa == sb);
String sc = "hello";
String sd = "hello";
System.out.println(sc == sd);
//对象,从内存分析
运行结果
hello
world
false
true
2.5布尔值扩展
代码
boolean flag = false;
if (flag == true) {
System.out.println('真');
} else {
System.out.println('假');
}
if (flag) {
System.out.println('真');
} else {
System.out.println('假');
}
//代码要精简易读
运行结果
假
假