从低到高的数据类型为:byte(字节型)-->shor(短整型) int(整型) long(长整型) float(单精度型) double(双精度)、char(字符型)
第一、任何数据类型碰到String类型的常量或者变量之后都向String类型转换:
1 public class demo02 { 2 3 public static void main(String[] args) { 4 String str = "zhangsan"; 5 int x = 90; 6 str = str + x; 7 System.out.println("str = " + str); 8 } 9 }
输出结果为:str = zhangsan90
1 public class demo03 { 2 public static void main(String[] args) { 3 int i = 1; 4 int j = 2; 5 System.out.println("1 + 2 = " + 1 + 2); 6 } 7 }
输出结果为:1 + 2 = 12
第二、强制类型转换和自动类型转换
1 public class demo01 { 2 public static void main(String[] args) { 3 //自动类型转换 4 int n=2,m=3; 5 long L=4L; 6 float f=5.6f; 7 double d=0d; 8 d=m+n+L+f; 9 System.out.println("2+3+4+5.6f的和是:"+d); 10 //强制类型转换 11 int result=(int)d; //(int)针对大的数据类型进行取整 12 System.out.println("使用整形表示2+3+4+5.5的和是:"+result); 13 } 14 }
输出结果为:
2+3+4+5.6f的和是:14.600000381469727
使用整形表示2+3+4+5.5的和是:14