Don't be influenced by the idea of Java programming. Terminology in computer science- by reference (pass-by-reference), do not become self-speaking personal language. These terms are also not specific to Java, and you should not learn from a Java book the special "pass-by-reference" that cannot be used in C, C + +, or Fortran languages.
Verifying that passing by value is very simple, use an assignment statement in the method body, and take the parameter as the left value . When passed by value, the assignment of a parameter does not affect the argument, that is, the assignment statement does not have any effect.
For Foo (a A), note that you want to play a= new A () instead of playing another thing, such as A. Change ().
This text seconds understand, this section of the content can be skipped.
By defining a series of methods, you can break up your programs into small modules, and method calls connect them. A formal parameter is specified when the method is defined, and the formal parameter is initialized by the given actual parameter when the method is called.
An important topic in messaging is how message parameters (arguments) should be passed to the parameter of the method. in various programming languages, parameters are passed in a variety of ways [1]. This is by the language of the designer and the implementation of the choice. The commonly used parameters are passed by value (Pass-by-value) and by reference (pass-by-reference).
From the source of the parameter passing mechanism, the parameters in C are passed by value, and Fortran is passed by reference, while both are used in C + + language. The Java language, like the C language, takes a unique way of passing parameters: by value.
The parameterization mechanism needs to consider two issues:
Formal parameter initialization,
Whether the operation on the formal parameter in the method body has side effects on the argument.
1. Method call Stack
Passing by value means that when a method is called, the actual argument (or expression) is evaluated first, the result value is copied, and the copied value is stored in the formal parameter. In short, passing by value is passing a copy.
In principle (method stack frames are explained in [7.4 Runtime Storage Management]), Java creates a new method frame every time the method is called. formal parameters (whether primitive or reference type variables) belong to their own method frames, and the space on which the parameter holds its value is allocated on the stack . The actual argument (or expression) can be either in the heap (the object's domain) or another method frame (another local variable), and the two are independent . When passed by value, if the method being called modifies the value of the formal parameter, only the copy is changed, and the original value of the (actual parameter) is unaffected.
Routine 3?13 method calls the package Oo;import static tips. print.*;p ublic class passbyvalue{ private void m (int x) { x + = 5; } private int max (int a,int b) { return (a>b? a:b); } public void Foo () { ? int i = 1,j the symbol before the =2;//code, representing the breakpoint int max = max (i,j); M (max); I=max;} }
Create an object and execute its foo () method, which is the execution of Foo (), as shown in 3-6. It reflects two points: (1) How a "larger code" is decomposed into more small fragments (methods), and how these fragments form a large whole--the hypothetical method m (int) and Max (Int,int) have very long code. (2) The execution flow of the method invocation.
Figure 3?6 Method Invocation Process
Foo () Executes the process: (1) Initializes local variables I and J; (2) int max = max (i,j), first the (return) value of the method Max (I,J), and assigns the value to the local variable max. In order to find the value of the method Max (I,J), the JVM creates a new method frame Max, which assigns the values of the local variables I and J of the previous frame foo to the parameters, and the Foo frame waits. Max executes the return 2,max frame is ejected, 2 is assigned to Max, (3) executes M (max), creates a new frame m, and assigns the value 2 of the argument max of frame foo to the parameter x,m frame although it changes the value of x, but does not affect the value of the argument.
If you are familiar with setting breakpoints in the BlueJ source editor when learning [2.3.4 creating objects], you can switch between two frames in the method frame call stack shown in 3-7 to observe that the actual participation parameters are allocated their own space in each frame.
Figure 3?7 switching between two frames
2. Only the Java language is passed by value
To learn how to pass the parameters of the Java language, verify 3 scenarios:
(1) For basic types of parameters, the operation of the parameter in the method body does not produce side effects.
(2) When an object is referenced as a parameter, the argument (reference) does not change ;
(3) However, the reference as the message receiver May have changed the content of the object it points to.
Routine 3?14 Pass-by-value
Package OO; Import tips. fraction; Import static tips. print.*; public class passbyvalue{ By reference as a parameter, still passed by value//////// private void change (fraction frrr) { FRRR = new fraction (11,55);//note here. } private void Doubleit (fraction f) {f.add (f); } public void Test () { Fraction f = new fraction (1,3); P (f+ ""); Change (f); PLN (f); f = 1/3 Vs 1/5 Fraction F2 = new fraction (1,3); Fraction temp = new fraction (F2); Doubleit (F2); Doubleit (temp); PLN (F2); } } |
Routines, the change (fraction) and Doubleit (fraction) methods take a fractional class variable as a parameter. The execution of the test () code shows that the assignment of the change (fraction) to the parameter does not affect the argument, whereas Doubleit (fraction) passes the message to the parameter, which causes the content of the object pointed to by the parameter (also the object that the argument points to) to change, thus producing side effects.
To avoid the possible side effects of a method call, you can take the following actions:
2 The object that the reference points to belongs to the invariant class, and the object (content) of the immutable class is immutable, such as String.
2 clones an object and passes its reference to the method.
3. Negative negative positive
Sometimes two errors are put together and are correct in effect. A typical error example is "Objects in Java are passed by reference." There are two purposes for introducing this error: (1) to indicate what is passed by reference, and (2) to emphasize that when referenced as a method parameter, the pass-through value still has side effects. →
Passing by reference means that the formal parameter of the method is only the alias of the actual parameter-the argument is not the value of its own, but the address passed to the parameter, both of which have the same data storage location. So any time the method changes the value of the formal parameter, it actually changes the value of the actual argument.
The reason that "objects in Java is passed by reference" is a negative error that comes from an easily misleading phrase: objects are passed by reference (you're passing objects by reference). The intent is that the objects in Java are not passed, but instead pass their references. But whether it is English or Chinese meaning, a little careless will be confused with pass-by-reference. The so-called negative positive, based on:
(1) The object can be passed. The object is not passed in Java , so this is the wrong assumption. The root cause is that people often mix terminology. "Passing objects to methods" is a common argument after all, see [2.4.2 Reference variables, references, and objects].
(2) The formal parameter and the argument have the same position. If both the formal parameter and the argument are objects, this is of course true. The problem is that formal parameters and arguments (not objects) are references, just as the left hand and right hand point to the same moon, but the left hand is not the right hand and the left hand is not the alias/nickname of the right hand.
The effect is correct: it is possible to modify the object's contents.
Figure 3?8 Not passing objects
In short, the correct argument is that the object's reference is passed by value (object references is passed by value).
Exercise 3-1: It may be said that "objects are passed by reference, while references are passed by value" is too much of a word, and no "object is passed by reference". How do you answer? |
Exercise 3-2: Why say "basic types are passed by value and references are passed by reference" is an error. |
Exercise 3-3: What delivery mechanisms should be used to pass serialized objects in a network program? Hint: Pass the reference semantics. |
[1] http://www.yoda.arachsys.com/java/passing.html, the various parameters passed the semantics, by reference to pass the purpose.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction to Programming (Java) 3.3.2 passing semantics by value