public static void main(String[] args){
final StringBuffer a=new StringBuffer("hello");
final StringBuffer b=new StringBuffer("world");
a.append(b);
System.out.println(a);
final String c = new String("hello");
final String d= new String ("world");
c.concat(d);
System.out.println(c);
}
final StringBuffer a=new StringBuffer("hello");
final StringBuffer b=new StringBuffer("world");
a.append(b);
System.out.println(a);
final String c = new String("hello");
final String d= new String ("world");
c.concat(d);
System.out.println(c);
}
helloc.concat(d) 没有返回给任何对象,所以等于没有。
楼主有心阿
顶
1楼 你给解释一下 为什么这里就是 hello world
final StringBuffer a=new StringBuffer("hello");
final StringBuffer b=new StringBuffer("world");
a=a.append(b);
System.out.println(a); final String c = new String("hello");
final String d= new String ("world");
c.concat(d);
System.out.println(c);
}
这样呢.
final StringBuffer a=new StringBuffer("hello");
final StringBuffer b=new StringBuffer("world");
a.append(b);
System.out.println(a); final String c = new String("hello");
final String d= new String ("world");
c=c.concat(d);
System.out.println(c);
}
再这样呢
和
a.append(b);
没有任何区别
这里的代码并没有改变a引用的对象啊!!
String是不能更改的所以当c.concat(d);的时候,c作为一个String是不可能变成concat以后的String的
从而没有变化
helloworld
hello
第二个和第三个有错误 :
关键字final表示这个变量只能被赋值一次,一旦被赋值之后,就不能再更改了。
所以我觉得这两句:
a=a.append(b);
c=c.concat(d);
不能通过编译。