注意細節
字符是char 類型,字符串是String 類型
1、數字拼接char,得到的還是數字,相當於和它的ASCII編碼相加(如果定義成String 會編譯錯誤)
2、數字拼接String,得到的是String
3、數字同時拼接char 和 String,就看和誰先拼接,和誰后拼接
4、String 拼接任何類型,得到的都是String
public static void main(String[] args) { String s1 = 1234 + '_' + "test"; System.out.println("s1 = " + s1); String s2 = 1234 + "_" + "test"; System.out.println("s2 = " + s2); String s3 = "test" + '_' + 1234; System.out.println("s3 = " + s3); String s4 = "test" + 1234 + 56789; System.out.println("s4 = " + s4); System.out.println('_'); System.out.println(0 + '_'); }
得到的結果是:
s1 = 1329test
s2 = 1234_test
s3 = test_1234
s4 = test123456789
_
95