自动类型转换
容量小的数据类型可以自动转换为容量大的数据类型。
/**
* 测试类型转换
* @author creep_creep_creep
*
*/
public class A_conversion {
public static void main(String[] args) {
int a=324;
long b=a;
double c=b;
//a=b;
//long d=3.23f;
float e=22333l;
//特例
byte f=123;
}
}
强制类型转换
强制类型转换,又被称为造型,用于显式的转换一个数值的类型。在有可能丢失信息的情况下进行的转换是通过造型来完成的,但可能造成精度降低或溢出。
语法格式:
(type)var
运算符“()”中的type表示将值var想要转换成的目标数据类型。
/**
* 强制类型转换
* @author creep_creep_creep
*
*/
public class A_conversion {
public static void main(String[] args) {
double x=3.14;
int x1=(int)x;
char b='a';
int c=b+1;
System.out.println(x1);//3
System.out.println(c);//98
System.out.println((char)c);//b
}
}
735

被折叠的 条评论
为什么被折叠?



