Java 的数据类型
Java的数据类型
-
基本类型
整形
byte 1字节
short 2字节
int 4字节
long num=30L 8字节
浮点型
float 4字节
double 8字节
字符型
char 2字节
布尔型
boolean true false 1位
-
引用型
- 接口
- 数组
- 类
Java 中的进制转换
- int num =8 //十进制
- int num =08 //八进制
- int num =0x8 //十六进制
浮点数注意
- 浮点数的显示位数有限不能做比较和精确计算
- 通过BigDecimal类进行准确计算
强制类型转换
int a = 12454;
double b = a;//低到高 自动转换
byte c = (byte)a;//高到低 强制类型转换
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println((int)19.6);
12454
12454.0
-90 //高到低可能存在内存溢出风险
19 //高到低有精度问题
操作大数时注意溢出问题!
```java
long money = 10_0000_0000;//大数可以用下划线分割
```
常量定义
final double PI = 3.14;//常量一般用大写字母
计算结果类型判断
public class Operator {
public static void main(String[] args) {
long a = 101000000012211212L;
int b = 20;
short c = 30;
byte d = 30;
double e=14.5;
System.out.println(a+b+c+d);//Long类型
System.out.println(a+b+c+d+e);//double类型
System.out.println(b+c+d);//int类型
System.out.println(c+d);//int类型
}
}
当有double参与计算时,得到的结果是double,long参与计算结果为long,其余结果都为int类型
public class Operator {
public static void main(String[] args) {
/*
二进制计算 与(&) 或(|) 非(~) 异或(^) 左移(<<) 右移(>>)
两个二进制异或相同位0,不同位1
*/
/*
0000 0010
0001 0000
0000 1000
0000 0010
*/
System.out.println(2<<3);
System.out.println(8>>2);
}
}
16
2
public class Operator {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a+b);
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
30
1020
30
字符串拼接顺序