Java基础-字符串连接运算符String link operator

          Java基础-字符串连接运算符String link operator

                                作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

  字符串链接运算符是通过“+”进行拼接的。

 

一.使用“+”进行字符串拼接

 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 }

二.字符串可以与基本数据类型进行链接

 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 }

 

posted @ 2018-04-01 22:44  尹正杰  阅读(3084)  评论(0编辑  收藏  举报