注册 登录
编程论坛 J2EE论坛

一道方法调用的笔试题

西鄙人 发布于 2010-11-08 14:33, 800 次点击

pubic static void main(String[] args){
    String str="hello";
    char[] c=new char[]{'a','b','c'};
    method(str,c);
    System.out.println("str="+str+" ****** c="+c.toString());
}

public void method(String str,char[] c){
    str="ok there";
    c[0]='g';
}


没有ide,用记事本编辑的,可能有细节错误,各位见谅。大致意思应该表达清楚了。

问输出结果是什么?
5 回复
#2
hugeannex2010-11-15 14:25
str=hello ****** c=g,b,c
#3
贾文慧2010-11-16 16:05
应该是这个吧,str=ok there ****** c={g,b,c}
#4
maxliz2010-11-17 03:20
hello****c=字符数组的地址
public丢了个"l"
调用方法要new对象 或者把方法写成静态的
根据程序执行流程 method方法调用后 str指向"go there"的引用被销毁 所以打印的str是指向"hello"的那个
如果你把打印语句写在method方法里 最后打印出来的就是go there
字符数组你不重写他的toString方法 打印的都是内存地址 你里面的c.toString和直接打印c没区别


#5
czc08072010-12-07 12:28
楼上正解 。
#6
justbelyf2010-12-13 17:05
第一,public写错了
第二,method不是static,不能直接调用
第三,java中只有值传递
所以输出hello****abc
1