public class TempTest { private void test1(int a){ //做点事情 } public static void main(String[] args) { TempTest t = new TempTest(); int a = 3; t.test1(a);//这里传递的参数a就是按值传递 } }
按值传递重要特点:传递的是值的拷贝,也就是说传递后就互不相关了。 示例如下:
public class TempTest { private void test1(int a){ a = 5; System.out.println("test1方法中的a==="+a); } public static void main(String[] args) { TempTest t = new TempTest(); int a = 3; t.test1(a);//传递后,test1方法对变量值的改变不影响这里的a System.out.println(”main方法中的a===”+a); } }
} public static void main(String[] args) { TempTest t = new TempTest(); A a = new A(); t.test1(a); //这里传递的参数a就是按引用传递 } } class A{ public int age = 0; }
第1行 public class TempTest { 第2行 private void test1(A a){ 第3行 a.age = 20; 第4行 System.out.println("test1方法中的age="+a.age); 第5行 } 第6行 public static void main(String[] args) { 第7行 TempTest t = new TempTest(); 第8行 A a = new A(); 第9行 a.age = 10; 第10行 t.test1(a); 第11行 System.out.println(”main方法中的age=”+a.age); 第12行 } 第13行 } 第14行 class A{ 第15行 public int age = 0; 第16行 }
第1行 public class TempTest { 第2行 private void test1(A a){ 第3行 a = new A();//新加的一行 第4行 a.age = 20; 第5行 System.out.println("test1方法中的age="+a.age); 第6行 } 第7行 public static void main(String[] args) { 第8行 TempTest t = new TempTest(); 第9行 A a = new A(); 第10行 a.age = 10; 第11行 t.test1(a); 第12行 System.out.println(”main方法中的age=”+a.age); 第13行 } 第14行} 第15行class A{ 第16行 public int age = 0; 第17行}
我见过的强调java只有值传递的书: Core Java 作者 : Cay S.Horstmann,Gary Cornell THE Java™ Programming Language By Ken Arnold, James Gosling, David Holmes
2.6.5. Parameter Values All parameters to methods are passed "by value."
Java in a Nutshell, By David Flanagan
2.9.4. Terminology: Pass by Value I've said that Java handles objects "by reference." Don't confuse this with the phrase "pass by reference." "Pass by reference" is a term used to describe the method-calling conventions of some programming languages. In a pass-by-reference language, valueseven primitive valuesare not passed directly to methods. Instead, methods are always passed references to values. Thus, if the method modifies its parameters, those modifications are visible when the method returns, even for primitive types.
Java does not do this; it is a "pass by value" language. However, when a reference type is involved, the value that is passed is a reference. But this is still not the same as pass-by-reference.
The Java Tutorials Passing Reference Data Type Arguments Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.
4、为什么“按引用传递”是错误的?[不要讨论它错误之处,而是问一问他们的思路为什么错误】? 其实关键问题就是这个术语的定义: In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. java中明显处理的是copy,而不是原来的引用本身,从这个角度说,“pass by reference”这个术语是不合适的。
但我想问题还是出在java设计时的术语选择上,比如Object obj;把这个obj称为“引用”,同时强调传参数时不是"pass by reference",本身就怪怪的。
我想这也是错怪了Thinking in Java了,我看的是第四版,里面有说明: Although you treat everything as an object, the identifier you manipulate is actually a “reference” to an object. 对这句话有注释: 1 This can be a flashpoint. There are those who say, “Clearly, it’s a pointer,” but this presumes an underlying implementation. Also, Java references are much more akin to C++ references than to pointers in their syntax. In the 1st edition of this book, I chose to invent a new term, “handle,” because C++ references and Java references have some important differences. I was coming out of C++ and did not want to confuse the C++ programmers whom I assumed would be the largest audience for Java. In the 2nd edition, I decided that “reference” was the more commonly used term, and that anyone changing from C++ would have a lot more to cope with than the terminology of references, so they might as well jump in with both feet. However, there are people who disagree even with the term “reference.” I read in one book where it was “completely wrong to say that Java supports pass by reference,” because Java object identifiers (according to that author) are actually “object references.” And (he goes on) everything is actually pass by value. So you’re not passing by reference, you’re “passing an object reference by value.” One could argue for the precision of such convoluted explanations, but I think my approach simplifies the understanding of the concept without hurting anything (well, the language lawyers may claim that I’m lying to you, but I’ll say that I’m providing an appropriate abstraction).
The Java Programming Language虽然强调pass by value,但是也支持不精确的表达,要不你每说一句话都强调,岂不是太累了. 1.7.1. Creating Objects Most of the time, you can be imprecise in the distinction between actual objects and references to objects. You can say, "Pass the object to the method" when you really mean "Pass an object reference to the method." We are careful about this distinction only when it makes a difference. Most of the time, you can use "object" and "object reference" interchangeably. 其实翻译成中文,Pass an object reference to the method.和pass by reference还有多大区别呢?
我们都会混用一些术语,没有人喜欢罗罗嗦嗦. 如The Java Programming Language,混用了objects and references(to objects), Thinking in Java混用了什么?identifier (you manipulate) is (actually a) “reference” (to an object.)
关于Thinking in Java,假设世界上没有C++,只有C和fortran语言,我不知道他说的那些东西那里准确.
Pass an object reference to the method.和pass by reference还有多大区别呢? 如果你把所有的接口都认为是Java的接口,你就知道"接口和实现分离"是多么难以解释/理解.pass by reference是计算机语言中的一般术语,不是Java的.
[最后,没有必要强迫别人接受你的思想和概念.用java设计好程序比什么都强.] 常常听人说基础很重要,假设pass by reference这是一个基础问题,不存在强迫别人接受谁的思想和概念,它不包含新的概念,不过是在"when it makes a difference." We are careful about this distinction .