There is no doubt that the difference between a function pass and a function reference is that it is possible to manipulate an object by passing a copy of the data to the reference.
But what most people ignore is that a reference is actually a copy of a reference.
Examples are as follows:
1 class Number2 {3 Public intnum;4 }5 Public classTopcoder6 {7 Public Static voidMain (string[] args)8 {9Number a=NewNumber ();TenA.num=1; OneNumber b=NewNumber (); Ab.num=2; - Swap (A, b); -System.out.println (a.num+ "" +b.num); the } - - Static voidSwap (number A,number b) - { + Number temp; -temp=b; +b=A; AA=temp; at } -}
Output is:
1 2
The analysis is as follows:
Before swap
Reference a----------->number (num:1)
Reference b----------->number (num:2)
In swap
Because the function simply passes a copy of the reference and does not have any effect on references A and B, it can be ignored.
After swap
Since the SWAP function has no effect on references A and B, and the swap function does not change the object content, there is no change
Different instances of value types and reference types in Java (iii)