黑马程序员技术交流社区
标题:
String做为形参和StringBuffer作为形参
[打印本页]
作者:
xiaoya0o0o
时间:
2015-9-19 21:27
标题:
String做为形参和StringBuffer作为形参
/*
* 看程序写结果:
* String作为参数传递:
* StringBuffer作为参数传递:
*
*/
public class Demo {
public static void main(String[] args) {
String str = "Hello";//str = 0x2233
fun1(str);//fun1(0x2233);
System.out.println("str = " + str);//Hello
StringBuffer buf = new StringBuffer("Hello");//0x7788
fun2(buf);//fun2(0x7788)
System.out.println("buf = " + buf);//HelloWorld
for(int i = 0 ; i < str.length() / 2 ; i++){
char c1 = str.charAt(i);
char c2 = str.charAt(str.length() - 1 - i);
if(c1 != c2){
System.out.println("不是对称串");
}
}
}
public static void fun1(String str){//str = 0x2233
str = "World";//"World"被开辟新空间存储:str = 0x4455
}
public static void fun2(StringBuffer buf){//buf = 0x7788
buf.append("World");//0x7788.append("World");
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2