Let's take a look at the C language code below.
#include <stdio.h>void s(char *b){ printf("%s\n",b); b="aaaa"; printf("%s\n",b);}int main(){ char *a="hello"; s(a); printf("%s\n",a);}
The output of my first impression should be changed
Hello
Aaaa
Aaaa
But this is wrong.
The actual output is
Hello
Aaaa
Hello
Isn't it weird? Actually Think About It. C language parameters are actually passed values (pointers are also passed pointer values). When s (a) is called, the stack opens up a parameter zone to pass the pointer value of. When the statement B = "aaaa"; is executed, the B pointer value in the parameter area is changed to the address of "aaaa", but the value of a is not changed, therefore, when the main function runs printf ("% s \ n", a);, the original value is printed. Let's look at the java code.
public void extractXPS(StringWriter sw) { sw=new StringWriter(); sw.write("Hello World");}void m(){ StringWriter sw=new StringWriter(); System.out.println(sw.toString()); }
What kind of output will be produced by the execution method m?
Only "" is output, that is, "Hello World" is not output ".
In java and C #, the object parameter actually passes a reference (= pointer), which can be modified. However, if you point to another object again, after jumping out of this method, it does not affect the original objects in the original caller.