Basic data type parameter passing
Class Demo
{public
static void Main (String [] args)
{
int x = 3;
Show (x);
System.out.println ("x=" +x);
}
public static void Show (int x)
{
x=4;
}
}
Analysis: Loads the Main method into the stack and assigns an int type of variable x, assign a value of 3, then call the Show method into the stack, assign the value of x 3 to the parameter x, and then assign the 4 to X, and then execute return (we've omitted to write, actually exist), So the show method is on the stack, which means there is no show () method in memory, so the value of the output X is x=3
So the change of the value of X is the x=3->x=4 (before the stack)->x=3 (the value of the output after the stack)
As shown in the figure:
Reference data type parameter passing
Class Demo
{
int x = 3;
public static void Main (String [] args)
{
Demo d = new demo ();
d.x = 9;
Show (d);
System.out.println (d.x);
}
public static void Show (Demo d)
{
d.x = 4;
}
}
parsing: int x = 3 is a member variable, so there is an initialization value of 0, the 3 assignment was then given to X, the X=3;main method loaded into the stack, the new object D in the heap memory, and the address value to D (for example, 0x0078);d. x = 9, which means that 9 is assigned a value of 3; Then execute the Show method, load into the stack, 4 assigned to X, then execute the return stack, the final printout x=4; The value of the
x changes: x=0 (initialized)->x=3 (first assignment)->x=9
->x=4;
as shown in the picture: