When calling a function, the passed argument may have been passed in the past, or it might have been a pass-through. If the value is passed, the operation inside the function has no effect on the value of the parameter, and if it is a pass-through, the operation inside the function is to manipulate the memory pointed to by the parameter, affecting the value of the parameter.
Is Java really a value or a pass-through? Use the following example to experiment:
Packagetest;Importjava.util.ArrayList;Importjava.util.List; Public classvalueandaddress { Public Static voidF1 (intx) {x= x + 5; return; } Public Static voidF2 (String s) {s=NewString ("10000"); return; } Public Static voidF3 (list<integer>list) {List.remove (0); List.remove (1); List.remove (2); return; } Public Static voidMain (string[] args) {intx = 5; String s=NewString ("ABCdef"); List<Integer> list =NewArraylist<integer>(); List.add (0); List.add (1); List.add (2); List.add (3); List.add (4); F1 (x); F2 (s); F3 (list); System.out.println ("X=" +x); System.out.println ("S=" +s); for(inti=0; I<list.size (); i++) {System.out.println ("list[" + i + "]=" +List.get (i)); } }}
Operation Result:
It can be seen that the value of the parameter of int and string is not changed, it is a pass value, and the parameter value of the list type has changed, is the address.
Java Transmit value and address