字符串拼接是我們?cè)?a href="/article/37106.html">Java代碼中比較經(jīng)常要做的事情,就是把多個(gè)字符串拼接到一起。都知道,String 是 Java 中一個(gè)不可變的類,所以一旦被實(shí)例化就無法被修改。
注意細(xì)節(jié)
字符是char 類型,字符串是String 類型
1、數(shù)字拼接char,得到的還是數(shù)字,相當(dāng)于和它的ASCII編碼相加(如果定義成String 會(huì)編譯錯(cuò)誤)
2、數(shù)字拼接String,得到的是String
3、數(shù)字同時(shí)拼接char 和 String,就看和誰先拼接,和誰后拼接
4、String 拼接任何類型,得到的都是String
代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
|
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 + '_' ); } |
得到的結(jié)果是:
s1 = 1329test
s2 = 1234_test
s3 = test_1234
s4 = test123456789
_
95
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/acm-bingzi/p/java_char_string.html