java.lang.String +concat(s1: String): String

来源:互联网 发布:比特币 软件 编辑:程序博客网 时间:2024/05/21 10:15

这是一段很有意思的代码

String类的Contact()方法:

package com.party90.res;public class test {    public static void main(String[] args) {        String str1 = "hello";        //这样操作 str本身并不会改变,只有str1=str1.concat(" world"); 将新生成的对象引用赋给str1。        str1.concat(" world"); 以前的hello 就废弃了。        String str2 = "hello";        str2 += " world";        str1.split(" ");        str2.split(" ");        for (int i = 0; i < str1.split(" ").length; i++) {            String s = str1.split(" ")[i];            System.out.print(s + "--");        }        System.out.println("\n");        for (int i = 0; i < str2.split(" ").length; i++) {            String s = str2.split(" ")[i];            System.out.print(s + "-");        }    }}---输出:hello--hello-world----更改后输出:hello--world--hello-world-
这样就一致了。
0 0
原创粉丝点击