First understand the meaning of references in C + +: "References" is "aliases". The reference in C + + represents the actual storage space. It is the operation of the storage space.
and the reference in Java: Can be seen as a "pointer" or "address" in the C language. It is valid to manipulate the properties referenced in Java (that is, the storage space that the pointer points to).
1) Java Reference as function (method) parameter
Java's method parameter is simply a value, when used as a parameter, it gives the copy of the value referenced in the function, so it makes no sense to exchange two reference parameters within a function, because the function exchanges the copy value of the parameter, but it makes sense to change the property of a reference parameter within the function. Because the copy value of the reference parameter points to the object and the original reference points to the same object.
2) C + + reference as function parameter
Because C + + references are "aliases", all operations performed within a function will directly act on the actual object storage space.
The reason for this confusion is likely to be related to the new heap space in the function (method) of malloc. In fact, this is a more interesting question.
There are no pointers in Java, and this inconvenience can be remedied by the Java Reference feature. That is, for any Java object, we can declare the object variable, but do not produce an instance, so that the variable to the actual instance of the object, you can implement the same instance object of multiple variable references, such as:
int x[]={1,2,3,4,5}, y[];
Y=x;
for (int i=0; i<y.length; i++) System.out.print ("" +y[i]);
A reference to x is completed by Y. From this actual effect, Y is very much like the pointer in C language here. but for "pointer" y we cannot do a + or-such arithmetic, that is, the Java reference can only point to the program-qualified existing objects that can be accessed, so the Java implementation considers it to be flexible and secure.
However, for C or C + + can be easily implemented through the function of the two-number exchange problem, that is, C + + functions as follows:
void swap (int &x, int &y) {int t; t=x; x=y; y=t;}
Is it possible to implement it in Java?
According to Java , the function parameters of Java are passed in two ways. For basic types, such as int,double, when passed as function parameters, the method of passing is used. For objects, such as arrays, strings, and so on as parameters, the use of reference method, that is, in the function of the passed object modification will completely affect the original object. Is it possible to exchange a value for an object using a reference? The following program fully demonstrates this problem:
public class Test {
public static void Main (string[] arg) {
int x1[]={1,2,3},x2[]={3,2,1};
Swap (X1,X2);//Swap function, which is the direct use of the parameter reference to Exchange
System.out.print ("swap->x1:\t"); Printarr (x1);
System.out.print ("swap->x2:\t"); Printarr (x2);
Swaparray (X1,X2);//Use the Swaparray function to swap with changes to the value of the object
System.out.print ("swaparr->x1:\t"); Printarr (x1);
System.out.print ("swaparr->x2:\t"); Printarr (x2);
Object T;
t=x1; x1=x2; X2= (int[]) t;//exchange directly with references in non-function calls
System.out.print ("tswap->x1:\t"); Printarr (x1);
System.out.print ("tswap->x2:\t"); Printarr (x2);
}
public static void Swap (object x, Object y) {//directly using parameter reference interchange
Object t=x;
X=y;
y=t;
}
public static void Swaparray (int x[], int y[]) {//Modify the value of the reference object to complete the interchange
if (x.length!=y.length) return;
int T[]=x.clone ();
for (int i=0; i<x.length; i++) x[i]=y[i];
for (int i=0; i<y.length; i++) y[i]=t[i];
}
public static void Printarr (int x[]) {//print array
for (int i=0; i<x.length; i++) System.out.print (x[i]+ "");
System.out.println ();
}
}
In the function swap, we swap directly with the parameters. In the function Swaparray, we swap by modifying the values of the two arrays that the parameters point to. In the case where the main function does not pass the function arguments, we make a direct exchange with the reference of the parameter.
The running results of the program are as follows:
Swap->x1:1 2 3
Swap->x2:3 2 1
Swaparr->x1:3 2 1
Swaparr->x2:1 2 3
Tswap->x1:1 2 3
Tswap->x2:3 2 1
from the running result we can clearly see that the function swap actually does not complete the exchange, while the function Swaparray and the exchange directly in main using the reference is successful. From here we know that although Java references can implement similar effects for C and C + + pointers, this reference exchange in the main function is proven. But once the parameters of the function are passed, the exchange of this kind of reference is effective. Although it is exchanged in the same way as the main function using the object reference T. Guess the reason (because I'm not the Java implementation), only that the reference variable in the function and the variable called in the main function are not the same. That is, when the swap function is called, although the X1 reference is passed to x,x2, Y,x and y are exchanged, but X1 and X2 are not exchanged. That is, when the function swap declares the arguments x and Y, the actual other actually generates references that are completely unrelated to X1 and x2, except that X and Y also point to X1 and X2, where the array object X1 and X2 have two pointers x, x1, and Y, and Y1 point to them respectively. Such a result would of course not accomplish a similar exchange as C and C + +. The only way to do this is to try to change the value of the two objects pointed to by the parameters to achieve the effect of the interchange, as in the Swaparray function.
So one of the related conclusions I came up with is that Java can never implement swap functions like C and C + +.
Java Exchange two numbers
There is no concept of pointers in Java, references in Java, but this concept has been watered down there are two ways to do this:
"1" uses global variables for numeric delivery
public class swap{
Public Swap (int x,int y) {
int z;
Z=x;x=y;y=z;
A=x; B=y;
}
public void GetInfo () {
SYSTEM.OUT.PRINTLN ("After Exchange: A=" +a+ ", b=" +b);
}
private static int A;
private static int B;
public static void Main (String args[]) {
a=10;
b=8;
System.out.printf ("Before Exchange: A=" +a+ ", b=" +b+ "\ n");
F w1=new F (A, b);
W1.getinfo ();
}
}
"2" takes the parameters of the interchange as a variable of the object
Class t{
int t;
}
public class G {
public static void Main (String args[]) {
T a=new t ();
T b=new t ();
a.t=8;
b.t=10;
System.out.println ("Before Exchange: A=" +a.t+ ", b= "+b.t);
Change (A, b);
SYSTEM.OUT.PRINTLN ("After Exchange: A=" +a.t+ ", b=" +b.t);
}
public static void Change (T a,t b) {
T c=new t ();
C.T=A.T; a.t=b.t; B.T=C.T;
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The differences between references in Java and references in C + +