当前位置: 技术问答>java相关
能帮我看看这道题吗?为什么选B?
来源: 互联网 发布时间:2017-03-28
本文导语: public class Foo { public static void main (String [] args) { StringBuffer a = new StringBuffer (“A”); StringBuffer b = new StringBuffer (“B”); operate (a,b); System.out.println{a + “,” +b}; } ...
public class Foo {
public static void main (String [] args) {
StringBuffer a = new StringBuffer (“A”);
StringBuffer b = new StringBuffer (“B”);
operate (a,b);
System.out.println{a + “,” +b};
}
static void operate (StringBuffer x, StringBuffer y) {
y.append {x};
y = x;
}
}
What is the result?
A.The code compiles and prints “A,B”.
B.The code compiles and prints “A, BA”.
C.The code compiles and prints “AB, B”.
D.The code compiles and prints “AB, AB”.
E.The code compiles and prints “BA, BA”.
F.The code does not compile because “+” cannot be overloaded for stringBuffer.
public static void main (String [] args) {
StringBuffer a = new StringBuffer (“A”);
StringBuffer b = new StringBuffer (“B”);
operate (a,b);
System.out.println{a + “,” +b};
}
static void operate (StringBuffer x, StringBuffer y) {
y.append {x};
y = x;
}
}
What is the result?
A.The code compiles and prints “A,B”.
B.The code compiles and prints “A, BA”.
C.The code compiles and prints “AB, B”.
D.The code compiles and prints “AB, AB”.
E.The code compiles and prints “BA, BA”.
F.The code does not compile because “+” cannot be overloaded for stringBuffer.
|
在JAVA中方法调中用的参数列表是“值传递”的,而这个传的是对象的reference,这和C++中的指针方式传递参数是相似的,你可以改变指针所指向的内存地址中的内容或调用指针所指对象的方法,但你不能改变指针本身。上例中所有对Y的reference的修改操作将只是方法内生效。
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。