Java基礎-字符串鏈接運算符String link operatorspa
做者:尹正傑code
版權聲明:原創做品,謝絕轉載!不然將追究法律責任。blog
字符串連接運算符是經過「+」進行拼接的。字符串
一.使用「+」進行字符串拼接io
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 public class StringDome{ 8 public static void main(String[] args){ 9 String Name = "Yinzhengjie"; 10 String Language = "Java"; 11 12 System.out.println( Name + Language ); 13 } 14 }
二.字符串能夠與基本數據類型進行連接class
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 public class StringDome{ 8 public static void main(String[] args){ 9 String Name = "Yinzhengjie"; 10 11 System.out.println( Name + 2018 ); //Yinzhengjie2018 12 System.out.println( Name + 4.1 ); //Yinzhengjie4.1 13 System.out.println( Name + 'Y' ); //YinzhengjieY 14 System.out.println( Name + true ); //Yinzhengjietrue 15 System.out.println( Name + 20 + 18 ); //Yinzhengjie2018 16 System.out.println( 2018 + 410 + Name ); //2428Yinzhengjie 17 18 } 19 }
三.把基本數據類型轉換爲字符串基礎
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 public class StringDome{ 8 public static void main(String[] args){ 9 // String Year = 2018; //"錯誤: 不兼容的類型: int沒法轉換爲String" 10 11 // String Year = "" + 2018; //與"String Year = 2018 + "";"的結果是一致的,將數字同化成字符串類型。 12 13 System.out.println(Year); 14 15 Year = "" + true; 16 17 System.out.println(Year); 18 19 } 20 }
四.變量參與字符串連接,實際上連接的變量的值變量
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 public class StringDome{ 8 public static void main(String[] args){ 9 String Name = "yinzhengjie"; 10 11 int Age = 18; 12 13 double salary = 1000000; 14 15 boolean isMarried = true; 16 17 System.out.println("Name:"+Name + ",Age:" + Age + ",salary:" + salary +",isMarried:"+isMarried); 18 /** 19 輸出結果:Name:yinzhengjie,Age:18,salary:1000000.0,isMarried:true 20 */ 21 } 22 }