类型转换
- 由于Java是强类型语言,所以在进行某些运算的时候,需要用到类型转换。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/758a8411d3bbb9556a91a7a3c006c5d5.png)
- 运算中,不同类型的数据先转化为同一类型,然后进行运算。
- 强制类型转换
- 自动 类型转换
public class Demo04 {
public static void main(String[] args) {
int i = 128;
byte b = (byte)i;
double c = i;
System.out.println(i);
System.out.println(b);
System.out.println(c);
System.out.println("==================");
System.out.println((int)23.7);
System.out.println((int)-45.89f);
System.out.println("==================");
char d = 'a';
int e = d+1;
System.out.println(e);
System.out.println((char)e);
}
}
运行结果:
128
-128
128.0
==================
23
-45
==================
98
b
public class Demo05 {
public static void main(String[] args) {
int money = 10_0000_0000;
int years = 20;
int total = money*years;
System.out.println(total);
long total2 = money*years;
System.out.println(total2);
long total3 = money*((long)years);
System.out.println(total3);
}
}
运行结果:
-1474836480
-1474836480
20000000000