Heis的Blog

保持简单,保持愚蠢
随笔 - 29, 文章 - 1, 评论 - 122, 引用 - 0
数据加载中……

Java方法中使用的是值传递(pass-by-value)!

   实在看不下去网上的一些面试题,很多都是错的答案。例如像今天这个问题:java方法用的是值传递还是引用传递。你在blogjava上还能搜到不同的答案呢。最近有空就翻译了一篇国外的文章,很多东西不能只看答案,而不知其所以然。第一次翻译文章,博友多指教。
    重申:对于原始类型(primitive type也译为值类型),是通过拷贝一个相同的值传给java方法的参数的;而对于引用类型(reference type),就是对象,是通过拷贝一个相同的应用或地址传给java方法的参数的。业界都统称这是pass-by-value(值传递),这里是翻译一篇 国外的文章来说明为什么java中的值传递比较特别。
    java中值传递比较特别,也比较有争议,所以重要的是要理解它的原理。推荐Java程序员都去读《Effective java》。


    原文地址:http://java.sun.com/developer/JDCTechTips/2001/tt1009.html
    另一篇写的比较好的文章(英文):http://www.brpreiss.com/books/opus5/html/page590.html
    blogjava上前辈的文章:http://www.blogjava.net/zygcs/archive/2008/10/05/232438.html

实际参数是如何传递到java方法的

HOW ARGUMENTS ARE PASSED TO JAVA METHODS

Suppose you're doing some Java programming, and you have a simple program like this:

假如你正在编写java程序,然后你写了以下一个程序:

 1 public class CallDemo1 {
 2         static void f(int arg1) {
 3             arg1 = 10;
 4         }
 5     
 6         public static void main(String args[]) {
 7             int arg1;
 8     
 9             arg1 = 5;
10     
11             f(arg1);
12     
13             System.out.println("arg1 = " + arg1);
14         }
15     }
16 

In the main method, the variable arg1 is given the value 5, and then passed as an argument to the method f. This method declares a parameter of the same name, arg1, used to access the argument.

main方法中,变量arg1赋值5,然后作为实际参数传递到方法f。这个方法声明一个相同名字的形式参数arg1,被用于访问实际参数

What happens when you run this simple program? The method f modifies the value of arg1, so what value gets printed, 5 or 10? It turns out that 5 is the right answer. This implies that setting arg1 to 10 in method f has no effect outside of the method.

当你执行这个程序会发生什么事情呢?方法f修改了arg1的值,所以会打印什么呢,5还是10?这里5是正确的答案。这就意味着在方法f中,将arg1设为10并不影响到方法的外部。

Why does it work this way? The answer has to do with the distinction between the pass-by-value and pass-by-reference approaches to passing arguments to a method. The Java language uses pass-by-value exclusively. Before explaining what this means, let's look at an example of pass-by-reference, using C++ code:

为什么呢?答案在于区分使用“值传递”方式和“引用传递”方式给方法传递参数。Java语言使用与众不同的“值传递”方式。在解释它的意思之前,让我们看看以下一个值传递的例子,这是用C++写的。

 1 #include <iostream>
 2     
 3     using namespace std;
 4     
 5     void f(int arg1, int& arg2)
 6     {
 7         arg1 = 10;
 8         arg2 = 10;
 9     }
10     
11     int main()
12     {
13         int arg1, arg2;
14     
15         arg1 = 5;
16         arg2 = 5;
17     
18         f(arg1, arg2);
19     
20         cout << "arg1 = " << arg1 << " arg2 = "<< arg2 << endl;
22     }
23 

Function f has two parameters. The first parameter is a pass-by-value parameter, the second a pass-by-reference one. When you run this program, the first assignment in function f has no effect in the caller (the main function). But the second assignment does, in fact, change the value of arg2 in main. The & in int& says that arg2 is a pass-by-reference parameter. This particular example has no equivalent in Java programming.

函数f有两个形参。第一个是值传递,第二个是引用传递。当你运行这个程序的时候,第一个参数赋值不影响调用者(main函数)。但是第二个参数赋值却会,事实上,修改了mainarg2的值。在int&中的&表示arg2是一个引用传递的形参。这个特别的例子与java有所不同

So what does pass-by-value actually mean? To answer this, it's instructive to look at some JVM1 bytecodes that result from the following two commands:

那值传递到底是什么意思?要回答这个问题,有需要看一下运行以下两行命令后的JVM的字节码

    javac CallDemo1.java

    javap -c -classpath . CallDemo1

Here is an excerpt from the output:

这里是输出的片段:

Method void f(int)

        
0 bipush 10

        
2 istore_0

        
3 return

    Method 
void main(java.lang.String[])

        
0 iconst_5

        
1 istore_1

        
2 iload_1

        
3 invokestatic #2 <Method void f(int)>


In the main method, the instruction iconst_5 pushes the value 5 onto the operand stack of the Java virtual machine. This value is then stored in the second local variable (arg1, with args as the first local variable). iload_1 pushes the value of the second local variable onto the operand stack, where it will serve as an argument to the called method.

main方法中,iconst_5指令将值5压入JVM的操作数堆栈(operand stack)。然后这个值随后存储第二个本地变量(arg1的值存在第一个本地变量处)。iload_1指令将第二个本地变量的值再压入操作数堆栈,作为被调用方法的形参。

Then the method f is invoked using the invokestatic instruction. The argument value is popped from the stack, and is used to create a stack frame for the called method. This stack frame represents the local variables in f, with the method parameter (arg1) as the first of the local variables.

然后方法f通过invokestatic指令调用。形参的值出栈,被用于为被调用的方法创建一个栈框架(stack frame)。这个栈框架表示所有f中的所有本地变量,而arg1实参将作为栈框架内的第一个本地变量。

What this means is that the parameters in a method are copies of the argument values passed to the method. If you modify a parameter, it has no effect on the caller. You are simply changing the copy's value in the stack frame that is used to hold local variables. There is no way to "get back" at the arguments in the calling method. So assigning to arg1 in f does not change arg1 in main. Also, the arg1 variables in f and in main are unrelated to each other, except that arg1 in f starts with a copy of the value of arg1 in main. The variables occupy different locations in memory, and the fact that they have the same name is irrelevant.

这里的的意思是形式参数作为实际参数的拷贝,被传入方法当中。如果你(在方法体内)修改了一个实际参数,它并不影响调用者,你只是修改了栈框架内的拷贝而已。不可能在调用方法中“取回”实参的值。因此在f中给arg1赋值不会改变main中的arg1,而且这两个arg1毫无关联,除了main中的arg1f中的arg1的一个拷贝之外。这两个变量存在内存的不同区域,事实上虽然它们有同样的名字,但是毫不相干。

By contrast, a pass-by-reference parameter is implemented by passing the memory address of the caller's argument to the called function. The argument address is copied into the parameter. The parameter contains an address that references the argument's memory location so that changes to the parameter actually change the argument value in the caller. In low-level terms, if you have the memory address of a variable, you can change the variable's value at will.

对比值传递,引用传递的参数是指传递调用者实参的内存地址给被调用的函数。实参地址拷贝给形参。形参指向了实参的内存区域,所以对形参的修改同样会修改到实参。从底层的角度来说,你可以任意修改变量的值,只要你有该变量的内存地址。

The discussion of argument passing is complicated by the fact that the term "reference" in pass-by-reference means something slightly different than the typical use of the term in Java programming. In Java, the term reference is used in the context of object references. When you pass an object reference to a method, you're not using pass-by-reference, but pass-by-value. In particular, a copy is made of the object reference argument value, and changes to the copy (through the parameter) have no effect in the caller. Let's look at a couple of examples to clarify this idea:

关于参数传递的讨论会复杂化,这是因为以上引用传递中的术语“引用”与Java编程中使用的(术语)存在微妙的区别。在java中,术语“引用”用于对象所有引用的上下文。当你给方法传递一个对象引用,你并不是使用引用传递,更像是值传递。尤其是,这样会拷贝一份实际参数的对象引用,(通过形参)对该拷贝的修改不会影响到调用者。让我们用以下两个例子来证明这个观点。


 1 class A {
 2 
 3         public int x;
 4 
 5         A(int x) {
 6             this.x = x;
 7         }
 8 
 9         public String toString() {
10             return Integer.toString(x);
11         }
12 
13     }
14 
15     public class CallDemo2 {
16 
17         static void f(A arg1) {
18             arg1 = null;
19         }
20 
21         public static void main(String args[]) {
22             A arg1 = new A(5);
23             f(arg1);
24             System.out.println("arg1 = " + arg1);
25         }
26 
27     }

In this example, a reference to an A object is passed to f. Setting arg1 to null in f has no effect on the caller, just as in the previous example. The value 5 gets printed. The caller passes a copy of the object reference value (arg1), not the memory address of arg1. So the called method cannot get back at arg1 and change it.

在这个例子中,A的引用传递给f。arg1设置为null不影响调用者,就像前一个例子一样。程序会打印5。调用者传递一份对象引用(arg1)的拷贝,而不是arg1的内存地址。所以调用方法无法取回到(调用者的)arg1变量并修改它。

Here's another example:


 1 class A {
 2 
 3         public int x;
 4 
 5         public A(int x) {
 6             this.x = x;
 7         }
 8 
 9         public String toString() {
10             return Integer.toString(x);
11         }
12 
13     }
14 
15    
16 
17     public class CallDemo3 {
18 
19         static void f(A arg1) {
20             arg1.x = 10;
21         }
22 
23         public static void main(String args[]) {
24             A arg1 = new A(5);
25             f(arg1);
26             System.out.println("arg1 = " + arg1);
27         }
28 
29     }



What gets printed here is 10. How can that be? You've already seen that there's no way to change the caller's version of arg1 in the called method. But this code shows that the object referenced by arg1 can be changed. Here, the calling method and the called method have an object in common, and both methods can change the object. In this example, the object reference (arg1) is passed by value. Then a copy of it is made into the stack frame for f. But both the original and the copy are object references, and they point to a common object in memory that can be modified.

这个例子会打印10。为什么呢?通过之前那个例子,你已经知道无法改变在调用方法中修改(实参)arg1。但是这里的代码显示arg1能够被修改。这里调用方法和被调用方法有个共同的对象,两个方法都能修改这个对象。在这个例子中,对象的引用(arg1)是值传递。他的一个拷贝被加入到f的栈框架。但是原来的和拷贝的对象都是对象引用,它们指向相同的内存区域中的对象,因此可以修改。

In Java programming, it's common to say things like "a String object is passed to method f" or "an array is passed to method g." Technically speaking, objects and arrays are not passed. Instead, references or addresses to them are passed. For example, if you have a Java object containing 25 integer fields, and each field is 4 bytes, then the object is approximately 100 bytes long. But when you pass this object as an argument to a method, there is no actual copy of 100 bytes. Instead, a pointer, reference, or address of the object is passed. The same object is referenced in the caller and the called method. By contrast, in a language like C++, it's possible to pass either an actual object or a pointer to the object.

java编程中,经常可以听到“一个String对象被传递到方法f”或者“一个数组被传到方法g”。从技术上来说,不是传递一些对象或数组,而是传递引用或地址。例如,如果你有一个java对象包含25个整型的属性,每个属性占4 字节,那这个对象大约是占100字节。但是作为形式参数传递的时候,不会拷贝100 字节。而是传递一个指针、引用或地址。这样调用者和被调用的方法都指向同一个对象。对比而言,与C++类似的语言,它可以传递真实的对象,也可以传递该对象的指针。

What are the implications of pass-by-value? One is that when you pass objects or arrays, the calling method and the called method share the objects, and both can change the object. So you might want to employ defensive copying techniques, as described in the September 4, 2001 Tech Tip, "Making Defensive Copies of Objects"

那什么是隐含的值传递呢?其一是当你传递对象或数组,由调用方法和被调用方法共享,双方都可修改对象。因此你可能想请入保护性拷贝技术,就像200194 日技术贴士“使用保护性拷贝技术。”

You can fix the case above, where the called method modifies an object, by making the class immutable. An immutable class is one whose instances cannot be modified. Here's how you to do this:

你可以修正以上的例子,当被调用方法修改一个对象时,让这个类成为不可变的。一个不可变类是指其实例不可修改的类。这里教你怎样去定义它。


 1 final class A {
 2 
 3         private final int x;
 4 
 5         public A(int x) {
 6             this.x = x;
 7         }
 8 
 9         public String toString() {
10             return Integer.toString(x);
11         }
12     }
13 
14    
15 
16     public class CallDemo4 {
17 
18         static void f(A arg1) {
19             //arg1.x = 10;
20         }
21 
22         public static void main(String args[]) {
23             A arg1 = new A(5);
24             f(arg1);
25             System.out.println("arg1 = " + arg1);
26 
27         }
28 
29     }



The printed result is 5. Now uncomment the modification of A in f and recompile the program. Notice that it results in a compile error. You have made A immutable, so it can't be legally modified by f.

打印结果是5,现在去掉CallDemo4f方法内的注释和重新编译。注意它会编译出错。因为你已经让A不可变了,所以f不能合法修改它。

Another implication of pass-by-value is that you can't use method parameters to return multiple values from a method, unless you pass in a mutable object reference or array, and let the method modify the object. There are other ways of returning multiple values, such as returning an array from the method, or creating a specialized class and returning an instance of it.

另一个隐含的值传递是你不能利用方法的形参在一个方法中返回多个值,除非你传入一个不可变对象引用或数组,然后让方法修改该对象。还有另外的方式返回多个值,例如从方法中返回一个数组,或者创建一个特别定义的类的实例。

For more information about how arguments are passed to Java Methods, see Section 1.8.1, Invoking a Method, and section 2.6.4, Parameter Values, in "The Java Programming Language Third Edition" by Arnold, Gosling, and Holmes. Also see item 13, Favor immutability, and item 24, Make defensive copies when needed, in "Effective Java Programming Language Guide" by Joshua

想知道更多关于Java方法中的实际参数的传递的信息,请看Arnold, Gosling, and Holmes的《The Java Programming Language Third Edition》中的1.8.1节“Invoking a Method”和2.6.4节“Parameter Values”。还有Joshua的《Effective Java Programming Language Guide条款13 Favor immutability”和24 Make defensive copies when needed”。

术语对照翻译

英文

中文

Argument

实际参数(实参)

Parameter

形式参数(形参)

Method

方法

Function

函数

pass-by-value

值传递

pass-by-reference

引用传递

operand stack

操作数堆栈

Stack frame

栈框架

Caller

调用者



程序员的一生其实可短暂了,这电脑一开一关,一天过去了,嚎;电脑一开不关,那就成服务器了,嚎……

posted on 2009-04-23 23:31 Heis 阅读(4978) 评论(7)  编辑  收藏 所属分类: 杂七杂八

评论

# re: Java方法中使用的是值传递(pass-by-value)!  回复  更多评论   

高深!
2009-04-24 09:28 | 淘声依旧

# re: Java方法中使用的是值传递(pass-by-value)!  回复  更多评论   

n年前就有人总结了啊,简单点说就两句话:
1. 基本类型:按值传递
2. 对象:将引用按值传递

搞清楚什么是对象,什么是引用就一切简单了。不明白的,参考一下c/c++中的指针。
2009-04-24 13:34 | sky ao

# re: Java方法中使用的是值传递(pass-by-value)!  回复  更多评论   

楼上正解,学习了!!
2009-04-24 14:02 | 杰德。张

# re: Java方法中使用的是值传递(pass-by-value)!  回复  更多评论   

@sky ao
精辟!
我个人认为很多技术问题不是只了解结论就行了。
2009-04-24 14:22 | Heis

# re: Java方法中使用的是值传递(pass-by-value)!  回复  更多评论   

这个是引用传递和值传递的争论是没有意义的,因为说的东西都是不一样的
2009-04-24 15:48 | 5452

# re: Java方法中使用的是值传递(pass-by-value)!  回复  更多评论   

@5452
在技术领域有统一的术语是非常重要的,当然术语背后的原理更为重要。
2009-04-24 22:00 | Heis

# re: Java方法中使用的是值传递(pass-by-value)!  回复  更多评论   

地球人都知道。
2009-04-24 23:07 | 9527

只有注册用户登录后才能发表评论。


网站导航: