Value passing: (formal parameter type is the basic data type): When the method is called, the actual parameter passes its value to the corresponding formal parameter, the formal parameter only initializes its own storage unit content with the value of the actual parameter, it is two different storage units, so the change of the formal parameter value in the method execution does not affect the value of the actual parameter.
A reference pass (a formal parameter type is a reference data type parameter): It can also be called an address pass. Method invocation, the actual parameter is an object (or an array), then the actual parameters and formal parameters point to the same address, in the execution of the method, the operation of the formal parameters is actually the operation of the actual parameters, the result is preserved after the end of the method, so the change in the method execution parameters will affect the actual parameters. Pointers and references look completely different (pointers are used with the operator "*" and "--", reference using the operator ". "), but Is that they seem to have the same function. Pointers and references let you refer to other objects indirectly. How do you decide at what time Use pointers, when to use references. First, realize that you cannot use a reference to a null value under any circumstances. A reference must always point to some Object. So if you use a variable and point it to an object, the variable may not point to the Any object, you should declare the variable as a pointer, because you can assign a null value to the variable. Conversely, if you change Amount must point to an object, such as your design does not allow the variable to be empty, then you can declare the variable as a reference.
|