1. Some definitions
Value passing: Assigns the value of the actual passed-in parameter to a copy in memory
Reference passing: The address of the actual parameter passed in
2. Pass the parameters of the basic type
Because Java is a value pass, it is not difficult to understand that the following code does not exchange any values
Swap (type arg1, type arg2) { = arg1; = arg2; = temp;}
3. Parameters for passing reference variables
Java uses references to manipulate objects, and all variables that point to the object are references, however, Java does not pass the reference to the parameter of the method, but rather to the value of the reference;
So why can the member values of an object be changed?
Class Apple {public String color= ' Red ', ' public ' class main {public static void main (string[] args) {Apple Apple = new APPL E (); System.out.println (Apple.color); Changeapple (Apple); System.out.println (Apple.color);} public static void Changeapple (Apple apple) {Apple.color = "green";}}
Because both the original and the assignment references point to the same object, the value of the member is changed
Output:
1 Red 2 Green
Reference passing and value passing of parameters in Java