/* 变量之间的运算:(不考虑 boolean。剩下: char byte short int long float double) 1,自动类型转换 2,强制类型转换 */ public class TestVeriablee { public static void main(String[] args) { //自动类型转换:当容量小的数据类型与容量大的数据类型做运算时候,容量小的会自动转换为容量大的数据类型。 //.byte,short ===> int ===> long ===> float === double ; int i1 = 12; //12 short s1 = 2; //2 int i2 = i1 + s1; //14 float f1 = 12.3F; //12.3 float f2 = f1 + i2; //26.3 long l = 12L; //12 float f3 = 1; //1 float f4 = l + f3; //13 System.out.println(f2); //26.3 System.out.println(f4); //13 //char c1 = 'a'; // 97 char c1 = 'A'; //65 int i3 = c1 +1; System.out.println(i3); //需要注意的是:当 char\byte\short之间做运算时,默认的结果为 int 类型; short ss1 = 12; byte bb1 = 1; char cc1 = 'a'; int ii1 = ss1 + bb1; int ii2 = cc1 + bb1; System.out.println(ii1); System.out.println(ii2); //2.强制类型转换:容量大转换为容量小的,要使用强制类型转换符:() //强制类型转换的问题:导致精读的损失; long l1 = 12345L; int m1 = (int)l1; System.out.println(m1); byte by1 = (byte)m1; System.out.println(by1); //平时常用的字符串,也是一种数据类型: String String nation = "我是一个中国人"; System.out.println(nation); //字符串与基本数据类型之间的运算:只能是连接运算:+。得到的结果仍为一个字符串; String str = "abc"; String str1 = str + m1; //abc12345 System.out.println(str1); //题目: String st111 = "Hello"; int myInt111 = 12; char ch111 = 'a'; //97 System.out.println(st111 + myInt111 + ch111); //hello12a System.out.println(myInt111 + ch111 + st111); //109hello System.out.println(ch111 + st111 + myInt111); //ahello12 } }
Java 基础进阶 07 -变量的自动类型转换+强制类型转换
猜你喜欢
转载自blog.csdn.net/u010282984/article/details/80721089
今日推荐
周排行