Key points and difficulties
* Method parameter transfer (emphasis, difficulty)
* 1. Formal parameters: method declaration, parameter in method parentheses
* Argument: The value of the actual parameter passed in when the method is called
*
* 2. Rule: Parameter passing mechanism in Java: value passing mechanism
* 1) A parameter is a basic data type: A variable that passes the value of an argument to the base data type of the parameter
* 2) Formal parameters are reference data types: The value of the argument's reference type variable (the first address value of the object entity corresponding to the heap space) is passed to the reference type variable of the formal parameter.
"Typical Example 1"
Public static void Main (string[] args) {
testargstransfer tt = new Testargstransfer ();
int i = ten;
int j = 5;
System.out.println ("I:" + i + "J:" + j);//i:10 J:5
////Exchange Variables I and J values
//int temp = i;
//i = j;
//j = temp;
Tt.swap (i, j);//Pass the value of I to the value of M,j to n
System.out.println ("I:" + i + "J:" + j);//i:10 J:5
}
//Define a method that swaps the values of two variables
Public void Swap (int m,int n) {
int temp = m;
m = n;
n = temp;
System.out.println ("m:" + M + "N:" + N);
}
After using the Swap method:
"Typical Example 2"
Public class TestArgsTransfer1 {
Public static void Main (string[] args) {
TestArgsTransfer1 tt = new TestArgsTransfer1 ();
dataswap ds = new Dataswap ();
System.out.println ("ds.i:" + ds.i + "DS.J:" + ds.j);
Tt.swap (DS);
System.out.println (DS);
System.out.println ("ds.i:" + ds.i + "DS.J:" + ds.j);
}
//Exchange The value of the element
Public void Swap (Dataswap d) {
int temp = D.I;
d.i = D.J;
D.J = temp;
System.out.println (d);//Print the value of the reference variable D
}
}
Class dataswap{
int i = 10;
int j = 5;
}
The value passing mechanism of Java