Java中说引用传递-这是不准确的
2013-11-07 21:18
204 查看
参考文章:http://www.javaranch.com/campfire/StoryPassBy.jsp
看如下代码:
运行结果:
t修改前a=2
doTest中a = 6
t修改后a=2
总结:java中只有值传递(pass by value),引用上面英文文章中的原话:
Java is pass-by-value.
For primitives, you pass a copy of the actual value.
For references to objects, you pass a copy of the reference (the remote control).
You never pass the object. All objects are stored on the heap. Always.
Now go have an extra big cup of coffee and write some code.
看如下代码:
package test; public class TestReference { int a,b; TestReference(int a,int b){ this.a = a ; this.b = b; } static void doTest(TestReference tr){ tr = new TestReference(6, 7); System.out.println("doTest中a = "+tr.a); } public static void main(String[] args) { TestReference t = new TestReference(2,3); System.out.println("t修改前a="+t.a); doTest(t); System.out.println("t修改后a="+t.a); } }
运行结果:
t修改前a=2
doTest中a = 6
t修改后a=2
总结:java中只有值传递(pass by value),引用上面英文文章中的原话:
Java is pass-by-value.
For primitives, you pass a copy of the actual value.
For references to objects, you pass a copy of the reference (the remote control).
You never pass the object. All objects are stored on the heap. Always.
Now go have an extra big cup of coffee and write some code.
相关文章推荐
- java中的值传递和引用传递问题
- java 值传递,和引用传递的问题
- java “值传递和引用传递”个人理解
- java基本数据类型传递与引用传递区别详解
- 访问控制符、代码块、引用传递、包 java (三)
- Java中的值传递和“引用传递”
- java中\值传递\“引用传递”
- Java 引用传递
- Java中参数的引用传递和值传递
- Java 引用传递
- java的"引用传递"
- Java中的值传递和引用传递
- Java里的按值传递与引用传递
- java 传值时可分为值传递与引用传递,那么他们有和区别
- Java中的值传递和引用传递
- Java的参数是按值传递的还是按引用?
- Java 引用传递
- 【java】值传递和引用传递理解
- java 值传递 引用传递 笔试题
- Java 值传递和引用传递