Write functions are unavoidable in Java development so parameter passing often bothers us, especially with the C + + foundation of the children's shoes always tangled in "Java is the value of pass or reference delivery?" "
First, a piece of code (and the programmer to communicate the best true or the code)
Public classTestmain { Public Static voidMain (string[] args) {List<Integer> list =NewArraylist<integer>(); for(inti = 0; I < 10; i++) {list.add (i); } add (list); for(Integer j:list) {System.err.print (J+",");; } System.err.println (""); System.err.println ("*********************"); String a= "A"; Append (a); System.err.println (a); intnum = 5; Addnum (num); SYSTEM.ERR.PRINTLN (num); } Static voidAdd (list<integer>list) {List.add (100); } Static voidAppend (String str) {str+ = "is a"; } Static voidAddnum (inta) {a=a+10; }}
the printed results are:
0,1,2,3,4,5,6,7,8,9,100,
*********************
A5
So it is said that the list is a reference to pass so can change their values, String, int is the value of the pass so can not change their values, this is it? Int can also say that because it is not an object, but list and string everyone is the object of what is a reference to pass a value is passed, look at the face? In fact, we may be confused by the concept of pointers in C/s + + because there are no pointers in Java:
According to Horstmann's "Java Core technology" (Chinese version 8th p115-p117) Description, Java is no reference to pass, the original text excerpt is as follows:
The Java programming language always takes a value call. That is, the method obtains a copy of all the parameter values, in particular, the method cannot modify the contents of any parameter variables passed to it. ”
"Some programmers (even the authors of this book) think that the Java programming language uses reference calls to objects, which is actually not true. ”
In fact, the Java function is passed the value, if I put the above code into the following code is estimated that everyone will not be confused (^ ^)
Public classTestmain { Public Static voidMain (string[] args) {List<Integer> list =NewArraylist<integer>(); for(inti = 0; I < 10; i++) {list.add (i); } {List <Integer> list1 = lsit; list1.add (100); } for(Integer j:list) {System.err.print (J+",");; } System.err.println (""); System.err.println ("*********************"); String a= "A"; {String A1 = A; A1 + = "is a"; } System.err.println (a); intnum = 5; { int a1 = num; a1 = A1 + Ten; } System.err.println (num); }}
The red part is the expanded function, the value of the variable in the parameter is the value of the external input, the function is modified inside the function of the value of the variable, so that the function can understand the parameters of what is passed?
In someone and you say value Pass and quote pass, directly paste his face it ~ ~ ~
parameter passing for Java functions