//trim()是用于删除前后多余的空格,composite.trim();以后为什么就剩hello等
What will be written to the standard output when the following program is run?public class Q03e4 {
  public static void main(String args[]) {
    String space = " ";
    String composite = space + "hello" + space + space;
    composite.concat("world");//composite变成 hello  world
    String trimmed = composite.trim();//????/
    System.out.println(trimmed.length());
  }
}
[Select One][1]5[2]6[3]7[4]12[5]13[Correct Choice]1
为什么是1呢,谢谢!

解决方案 »

  1.   

    String是不可变对象,concat返回一个新的String,BigDecimal等不可变对象与之类似
    String newString = composite.concat("world");
    newString "hello world"
    composite "hello"
      

  2.   


    composite.concat("world");//composite变成 hello  world
    //////////////////////////////////////////////////////返回一个对象是“hello  world ”
    但是composite自己没有变,仍然是:"  hello  "看一看API 写得很清楚//////////////////////
    Concatenates the specified string to the end of this string. 
    If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.Examples:  "cares".concat("s") returns "caress"
     "to".concat("get").concat("her") returns "together"