Java基础-字符串连接运算符String link operator
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
字符串链接运算符是通过“+”进行拼接的。
一.使用“+”进行字符串拼接
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ public class StringDome{
public static void main(String[] args){
String Name = "Yinzhengjie";
String Language = "Java"; System.out.println( Name + Language );
}
}
二.字符串可以与基本数据类型进行链接
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ public class StringDome{
public static void main(String[] args){
String Name = "Yinzhengjie"; System.out.println( Name + 2018 ); //Yinzhengjie2018
System.out.println( Name + 4.1 ); //Yinzhengjie4.1
System.out.println( Name + 'Y' ); //YinzhengjieY
System.out.println( Name + true ); //Yinzhengjietrue
System.out.println( Name + 20 + 18 ); //Yinzhengjie2018
System.out.println( 2018 + 410 + Name ); //2428Yinzhengjie }
}
三.把基本数据类型转换为字符串
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ public class StringDome{
public static void main(String[] args){
// String Year = 2018; //"错误: 不兼容的类型: int无法转换为String" // String Year = "" + 2018; //与"String Year = 2018 + "";"的结果是一致的,将数字同化成字符串类型。 System.out.println(Year); Year = "" + true; System.out.println(Year); }
}
四.变量参与字符串链接,实际上链接的变量的值
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ public class StringDome{
public static void main(String[] args){
String Name = "yinzhengjie"; int Age = 18; double salary = 1000000; boolean isMarried = true; System.out.println("Name:"+Name + ",Age:" + Age + ",salary:" + salary +",isMarried:"+isMarried);
/**
输出结果:Name:yinzhengjie,Age:18,salary:1000000.0,isMarried:true
*/
}
}