In the java method, the question of whether to pass the parameter value or the address is passed.
Question:
Compile a simple program for exchanging values. If we simply define an exchange function to receive two numbers, define an intermediate variable within the function to complete the exchange. When we pass the real parameters a and B to this function, we will not get the expected results. Why?
In C and C:
In C, if we run the following code:
#include<stdio.h> void swap(int,int); void main(){ int a=5; int b=7; printf("a=%d b=%d\n",a,b);//a=5 b=7 swap(a,b); printf("a=%d b=%d\n",a,b);//a=5 b=7 } void swap(int c,int d){ int temp; temp=c; c=d; d=temp; }
And run the following code in C # And try to transfer it to a and B:
Static void swap (int c, int d) // This function runs {int temp; temp = c; c = d; d = temp ;}
The result is not as expected to realize the numerical exchange between a and B.
This is because there are two different concepts in C and C #: value transfer and address transfer.
The above code implements the concept of "passing values. That is to say, when the program is running, the real parameter copies its own value to the form parameter, and the form parameter also allocates memory space. Therefore, after this value is passed, the real parameters and the form parameters actually point to two different buckets and there is no association relationship. In our so-called function for executing the exchange value, the exchange is actually a form parameter (not a, B, but c, d ), in addition, the life cycle of a local variable is only within the function, and the function stops running. Therefore, the Code cannot achieve our purpose of exchanging values.
To solve this problem, one of the solutions provided by C language and C # Is address transfer.
In C, we can easily use aliases to exchange two numbers:
# Include <stdio. h> void swap (int &, int &); void main () {int a = 5; int B = 7; printf ("a = % d B = % d \ n", a, B); // a = 5 B = 7 swap (a, B ); printf ("a = % d B = % d \ n", a, B); // a = 7 B = 5} void swap (int & a, int & B) {// here, the address is passed in using the address Operator
int temp; temp=a; a=b; b=temp; }
In C #, a special keyword ref is provided to indicate whether to pass a value or an address.
Static void swap (ref int I, ref int t) // This function will perform operations by addressing. {int temp; temp = a; a = B; B = temp ;}
The above two sections of code implement the address transfer method. We can implement our expected code implementation functions.
In the C language, you can also use the operation pointer method (but it is a value transfer) to achieve the expected
# Include <stdio. h> void swap (int *, int *); void main () {inta = 5; intb = 7; printf ("a = % d B = % d \ n ", a, B); // a = 5 B = 7 swap (& a, & B); // the address of a and B is transmitted here, use the address character printf ("a = % d B = % d \ n", a, B); // a = 7 B = 5} void swap (int *, int * B) {// enter the address to complete two statements: inttemp; temp = * a; * a = * B; * B = temp ;}
Whether it is an alias in C or a keyword ref in C. All parameters are passed through the real parameter address, that is, when the program is running, the real parameter passes its own address to the function. At this time, the real parameters and the actual parameters point to the same address, that is, an object in the memory space, then the operation on the form parameters during the function operation will naturally change the value of the real parameters.
What about java?
Add an example first:
package com.test;public class Test1 { public static void Reverse(int R[],int l,int r){ int i,j; int temp; for(i=l,j=r;i<j;++i,--j){ temp=R[i]; R[i]=R[j]; R[j]=temp; } } public static void Add(int a,int b){ a=a+100; b=b+1000; } public static void main(String[] args) { int[] array=new int[]{1,2,3,4,5,6}; Reverse(array,0,5); for(int i=0;i<array.length;++i) System.out.print(array[i]);
System.out.println(); System.out.println("——————————————"); int i=1,j=2; Add(i,j); System.out.println(i+"####"+j); }}
Output result:
654321——————————————1####2
Since programmers in Java cannot directly operate pointers, It is very vague for passing values or passing URLs. However, we know that variables in java are classified into two types: basic variables and references. (Arrays belong to references)
In fact, when we pass the real parameters a and B into the method, it is equivalent to passing the values of a and B to the method to receive the local variables of a and B respectively, the values of the real parameters a and B are not changed, because during the operation, the function receives the local variables of the values passed by a and B, and the function is executed completely, the two local variables do not exist.
So why can I modify the values of a and B by referencing them? Does this violate the principle described above?
In Java, both passing values and referencing are copies of the passed parameters. Real parameters are copied. The copy is only valid within the function. When a reference is passed in, the copy version of the address of the real parameter is passed in, and the address is copied, however, the point to the value has not changed (that is, for the "Address", there is a real parameter and a form parameter that does not point to the same bucket, but there is only one "address pointing value ), at the same time, the address cannot be changed, but the value pointed to by the address can be changed. So it can be said that all values are passed in java.
In C ++:
In C ++, the Operation pointer is allowed. There are also clear value transfer and address transfer. (In fact, the following two concepts are universally applicable to all these languages)
Value Transfer (pass by value): 1. Pass the real parameter value to the corresponding parameter 2. Pass the real parameter address to the corresponding parameter, such as an array or pointer. (Similar to Java)
Address Transfer (reference transfer): When an alias is used and the shared bucket (direct access) parameter is a reference parameter, it is passed by address. In this case, the corresponding real parameter is generally a variable.
Note: In addition to aliases, values are transmitted even if pointers are used.That is, in the parameter list of the function, as long as the storage space of the parameter is opened up, it is a value transfer.
Add two examples:
Example 1:
#include<iostream>using namespace std;void Reverse(int R[],int l,int r){ int i,j; int temp; for(i=l,j=r;i<j;++i,--j){ temp=R[i]; R[i]=R[j]; R[j]=temp; }}void Add(int c,int d){c=c+100;d=d+1000;}int main(){ int array[]={1,2,3,4,5,6}; Reverse(array,0,5); for(int i=0;i<6;++i){ cout<<array[i]; } cout<<endl; int a=1,b=2; Add(a,b); cout<<a<<","<<b; cout<<endl; return 0;}
Running result:
Example 2 (the code is from the reference blog ):
# Include <iostream> using namespace std; void mySwap (int * p1, int * p2); int main () {int a = 12; int B = 44; int * pa = & a; int * pb = & B; if (a <B) {mySwap1 (a, B); // mySwap2 (pa, pb ); // mySwap3 (pa, pb); // mySwap4 (a, B);} return 0;}/* the int type is used as the form parameter -- value passing: the form parameter, B also needs to allocate memory space. Copy the value of the real parameter to the form parameter */void mySwap1 (int a, int B) {int temp; temp = a; a = B; B = temp;}/* int * type as parameter -- value transfer: the parameter p1, p2 also needs to allocate memory space, and the value of the real parameter is copied to the parameter (address) */void mySwap2 (int * p1, int * p2) {// changing the pointer direction of the form parameter does not affect the real parameter int * temp; temp = p1; p1 = p2; p2 = temp;}/* int * type as the form parameter -- value transfer: the form parameter p1, p2 also needs to allocate memory space, and the value of the real parameter is copied to the form parameter (address) */void mySwap3 (int * p1, int * p2) {// change the memory space value pointed to by the form parameter pointer, which also changes the memory space value pointed to by the real parameter pointer int temp2; temp2 = * p1; * p1 = * p2; * p2 = temp2;}/* reference type as the form parameter -- Address Transfer: the form parameter a and B do not allocate memory space, the parameter is the "alias" */void mySwap4 (int & a, int & B) {int temp; temp = a; a = B; B = temp;
Summary:
C language, C #, and C ++ have clear value transfer and address transfer methods. Programmers can actively differentiate.
In C language and C ++, all aliases are passed by values.
In Java, because pointer cannot be operated directly, only value passing exists. However, when the passed value is a reference, the copied version of the referenced address is obtained in the method, the address has been copied, but the point to the value has not changed (that is, for the "Address", there is a real parameter and a form parameter that does not point to the same bucket, but there is only one "address pointing value"). At the same time, the address cannot be changed, but the address pointing value can be changed. So it can be said that all values are passed in java.
So back to the original question, how can we achieve the exchange of two values?
The answer in the editor is the operation address. Whether it is the value transfer or the address transfer, as long as the operation is the address, the form parameter and the real parameter point to the same bucket, that is to say, a change in the form parameter can bring about a change in the real parameter.
So, please think about how to implement the value exchange of basic variables in Java? I haven't thought about it yet. You are welcome to enlighten me.
Refer to blog:
Http://blog.csdn.net/single0515/article/details/51584536
Http://blog.csdn.net/cxc19890214/article/details/44617685
Http://blog.csdn.net/u010126792/article/details/63771475