数据类型转换
public class 类型转换 {
/*字节容量:byte、short、char-int-long-float-double(低——高)
运算中,不同类型的数据先转换为同一类型,然后进行运算
*/
//强制类型转换:高-低 (类型)变量名
public static void main(String[] args) {
int i = 128;
byte b = (byte)i;//内存溢出问题
System.out.println(i);
System.out.println(b);
//自动类型转换:低-高
int a = 128;
double c = a;
System.out.println(a);
System.out.println(c);
/*注意点
1.不能对布尔值进行转换
2.不能把对象类型转换为不相干类型
3.在把高容量转换为低容量的时候,需强制转换(类型)变量名
4.转换的时候可能存在内存溢出或精度问题
5.操作比较大的数,要注意溢出问题(数字之间可以用_分割)
6.long类型最好是采用大写L
*/
System.out.println((int)23.7);//23
System.out.println((int)-45.89f);//-45
int money = 10_0000_0000;//1000000000
System.out.println(money);
int money1 = 1000000000;
int years = 20;
int total = money1*years;//-1474836480,计算时溢出了
System.out.println(total);
long total1 = money1*years;//-1474836480,默认是int计算,转换前已存在问题
System.out.println(total1);
long total2 = money1*((long)years);//20000000000,把任意一个数转换为long类型
System.out.println(total2);
long total3 = ((long)money1)*years;//20000000000,把任意一个数转换为long类型
System.out.println(total3);
}
}
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现