值传递 & 引用传递
2016-05-02 20:02
260 查看
以下程序的输出结果是?
考察值传递和引用传递。对于值传递,拷贝的值用完之后就会被释放,对原值没有任何影响,但是对于引用传递,拷贝的是对象的引用,和原值指向的同一块地址,即操作的是同一个对象,所以操作之间会相互影响
所以对于String str是值传递,操作之间互不影响,原值保持不变。而ch是数组,拷贝的是对象的引用,值发生了改变,因此选择B
public class Example { String str = new String("good"); char[] ch = { 'a', 'b', 'c' }; public static void main(String args[]) { Example ex = new Example(); ex.change(ex.str, ex.ch); System.out.print(ex.str + " and "); System.out.print(ex.ch); } public void change(String str, char ch[]) { str = "test ok"; ch[0] = 'g'; } }
正确答案: B
A 、 good and abc
B 、 good and gbc
C 、 test ok and abc
D 、 test ok and gbc 解析:
考察值传递和引用传递。对于值传递,拷贝的值用完之后就会被释放,对原值没有任何影响,但是对于引用传递,拷贝的是对象的引用,和原值指向的同一块地址,即操作的是同一个对象,所以操作之间会相互影响
所以对于String str是值传递,操作之间互不影响,原值保持不变。而ch是数组,拷贝的是对象的引用,值发生了改变,因此选择B
相关文章推荐
- javascript入门篇之事件处理
- handler消息传递机制
- Python Pandas库 常见使用错误总结
- HDU 2586 LCA转RMQ
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />解释
- 经典SQL练习题4----面试题及相关知识
- [Interactive Python]Memory游戏 Mini-project #5
- 使用$this变量访问方法和属性
- LTE小区搜索-物理小区ID和同步信号PSS、SSS
- 欧拉函数
- C++中的栈和队列操作明细
- JDBC笔记-BLOB和获取主键
- leetcode笔记—Find the Duplicate Number
- HDOJ-2066-一个人的旅行
- redis-集合
- 经典SQL练习题3----student/score/cource/teacher
- HDU 5677 ztr loves substring
- Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation (贪心)
- Spring之集合DI
- 学习笔记之深入浅出MFC 第8章 C++重要特性----静态变量(static)