字符串拼接
由于String字符串是不可变字符串,所以String字符串进行拼接以后会产生一个新的对象。
1 使用 + 号(可以连接任何类型数据拼接成为字符串)
2 使用 concat(String str)方法(只能拼接String字符串)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| public class Test { public static void main (String[]args) {
String s1 = "Hello"; String s2 = s1 + " World"; System.out.println(s2);
String s4 = "Hello";
s4 += " "; s4 += "World"; System.out.println(s4);
String s5 ="Hello";
s5 = s5.concat(" ").concat("World!"); int age = 18; String s6 = "她的年龄是" + age + "岁"; System.out.println(s6);
char score = 'A'; String s7 = "她的英语成绩是" + score; System.out.println(s7);
java.util.Date nowDate = new java.util.Date();
String s8 = "今天是: " + nowDate; System.out.println(s8); } }
|