The data types in Java are divided into basic data types and reference data types, and the parameters of the methods are divided into actual parameters and formal parameters.
- Formal parameters: Parameters that are written when the method is defined.
- Actual parameter: The specific value written when the method is called.
In general, when data is passed as a parameter, the base data type is the value pass, and the reference data type is the reference pass (address delivery).
one. Value passing
1 Public Static voidMain (string[] args) {2 intA = 2;3 intb = 3;4 Change (A, b);5System.out.println ("a =" +a);6System.out.println ("b =" +b);7 }8 Public Static voidChangeintA1,intB1) {9a1++;Tenb1++; OneSystem.out.println ("a =" +A1); ASystem.out.println ("b =" +B1); -}
Operation Result:
1 A = 32 b = 43 a = 24 b = 3
Process:
1. Main function into the stack, a, b initialization;
2. Call the change method, make a move to the stack, and copy the values of A and B to A1 and B1;
The A1 and B1 are calculated in the 3.change method.
4.change method bomb Stack;
5. main function stack;
Cause Analysis:
In the change method, the assignment of A1 and B1 does not affect A, B, because the value in A1, B1, is only copied from a, B, that is, A1, B1 equivalent to a, b copy, the contents of the copy, no matter how modified, will not affect the original itself.
two. Reference Delivery
1 Public Static voidMain (string[] args) {2 int[] A = {1, 2, 3, 4, 5};3 Change (a);4System.out.println (a[0]);5 }6 Public Static voidChangeint[] Array) {7Array[0] = 0;8System.out.println (array[0]);9}
Run results
1 02 0
Process:
1. Main function in the stack, int[] a initialization;
2. Call the change method, and make a copy of the address value of a to array;
In the 3.change method, the array in the heap is found based on the address value, and the value of the first element is changed to 0.
The 4.change method has finished running, and the value of the first element in the array has changed.
5.change method bomb Stack;
6. Main function stack;
Cause Analysis:
When the change () method is called, the parameter array receives a copy of the A address value. And in the change method, the array is manipulated by the address value. After the change method is stacked, the values in the array have changed. Both the main function and the change method operate on an array that corresponds to the same address value.
three. String type passing
1 Public Static voidMain (string[] args) {2String a = "123";3 Change (a);4 System.out.println (a);5 }6 Public Static voidChange (String A1) {7A1 = "321";8 System.out.println (A1);9}
The operating result is:
1 3212 123
Process:
1. Main function into the stack, a initialization;
2. Call the change method, and make a copy of the address value of a to A1;
3.change method, a String object "321" is recreated and the A1 points to the new address value;
4.change method is finished, the address value pointed to by A1 has changed;
5.change method bomb Stack;
6. Main function stack;
Cause Analysis:
When a string object is passed as a parameter, the walk is still a reference pass, except that the class string is special.
once a string object is created, the content cannot be changed. Each change in content is a new object that is recreated.
When the change method finishes executing, the address value pointed to by A1 has changed. A1 's original address value is copy-copied, so it doesn't change the value of a.
cases similar to string type
1 Public classTest3 {2 3 Public Static voidMain (string[] args) {4Person p =NewPerson ("Xiaoming");5 Change (p);6 System.out.println (p.name);7 }8 Public Static voidChange (person p1) {9Person person =NewPerson ("Little Red");TenP1 =Person ; One System.out.println (p1.name); A } - } - classPerson { the String name; - PublicPerson (String name) { - This. Name =name; - } +}
The result of the operation is:
1 Little Red 2 xiaoming
Summary:
- When the value is passed, a copy of the value of the argument is copied to the parameter.
- When a reference is passed, the address value of the argument is copied to the parameter.
Several cases of JAVA value passing and reference passing