Java数据类型转换
原创约 358 字大约 1 分钟...
186-Java数据类型转换.md
注意
本博文仅供学术研究和交流参考,严禁将其用于商业用途。如因违规使用产生的任何法律问题,使用者需自行负责。
概念




示例代码
package com.tencent.chapter02;
/**
* <p>Title: DataTypeConverson_04</p>
* <p>
* Description:
* </p>
* @author xianxian
* @date 2023年1月3日
*/
public class DataTypeConverson_04 {
/**
* <p>Title: main</p>
* <p>
* Description:
* </p>
* @param args
*/
public static void main(String[] args) {
/**
* 自动类型转换(隐式转换): 小 -> 大,类型兼容
*
* 特殊:long -> float
* 注意:byte、short、char之间不会相互转换,他们三者在计算时首先会转换为int类型
*/
//int -> double
int num = 10;
double dnum = num;
System.out.println("num:" + num);//10
System.out.println("dnum:" + dnum);//10.0
//long -> float
long l = 1234567890123456789L;
float f = l;
System.out.println("l:" + l);//1234567890123456789
System.out.println("f:" + f);//1.23456794E18
//byte、short、char之间不会相互转换,他们三者在计算时首先会转换为int类型
byte b1 = 125;
byte b2 = 120;
int sum = b1 + b2;
/**
* 强制类型转换(显示转换): 大 -> 小,可能类型不兼容
*
* 风险:① 精度下降 ② 数字溢出
*/
byte b3 = 125;
byte b4 = 120;
byte sum2 = (byte) (b1 + b2);
System.out.println(sum2);//-11
}
}
分割线
相关信息
以上就是我关于 Java数据类型转换 知识点的整理与总结的全部内容,希望对你有帮助。。。。。。。
Powered by Waline v2.15.4