Java數(shù)字和字符串拼接原理及案例
字符串拼接是我們在Java代碼中比較經(jīng)常要做的事情,就是把多個字符串拼接到一起。都知道,String 是 Java 中一個不可變的類,所以一旦被實(shí)例化就無法被修改。
注意細(xì)節(jié)
字符是char 類型,字符串是String 類型
1、數(shù)字拼接char,得到的還是數(shù)字,相當(dāng)于和它的ASCII編碼相加(如果定義成String 會編譯錯誤)
2、數(shù)字拼接String,得到的是String
3、數(shù)字同時(shí)拼接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 + ’_’);}
得到的結(jié)果是:
s1 = 1329tests2 = 1234_tests3 = test_1234s4 = test123456789_95
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. html中的form不提交(排除)某些input 原創(chuàng)2. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享3. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式4. jsp文件下載功能實(shí)現(xiàn)代碼5. 開發(fā)效率翻倍的Web API使用技巧6. ASP常用日期格式化函數(shù) FormatDate()7. js select支持手動輸入功能實(shí)現(xiàn)代碼8. CSS3中Transition屬性詳解以及示例分享9. asp.net core項(xiàng)目授權(quán)流程詳解10. CSS3實(shí)現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效
