The problem stems from using objects as parameters in Java, debugging in C + +, and discovering that the results are not the same as in C + +.
The problem is caused by the fact that Java and C + + interpret objects differently.
In C + +, an object takes a "call to value" as a parameter, and when an argument is passed through a formal parameter, the copy constructor of the object (argument) is called (if the copy constructor is not explicitly defined, the default copy constructor is automatically called, and its function is to copy the object from the argument to the formal parameter, This involves deep copy and shallow copy issues, but does not affect the analysis of this problem. ), the function actually operates a copy of the object and does not affect the original object.
In Java, when an object is a parameter, the formal parameter is a reference that is initialized to the argument object. If you manipulate a parameter, it affects the argument (the original object). This is a bit like "reference invocation" in C + +, but this is not the case with "call to Value" in Java. The reason for the difference is that the interpretation of object names in Java and C + + is different. In C + +, the relationship between "class" and "object" can be likened to the relationship between "type" and "variable", where the object allocates memory space when it is defined, and it is actually present. In Java, when an object is defined, it simply defines an object variable, which simply points to an object that is constructed when the new operator is used. The object name in Java is a bit like a pointer to an object in C + +, and when passed as a parameter, the object's address in memory is passed.
The object str for the string class in C + + and Java is stored in the following ways:
An example of a Java object passed as a parameter is analyzed:
1 Public classTest1 {2 Public Static voidMain (string[] args) {3StringBuffer str =NewStringBuffer ("Hello");4System.out.println ("before change, str =" +str);5 changedata (str);6System.out.println ("after changedata, str =" +str);7 }8 9 Public Static voidChangedata (StringBuffer strbuf) {TenStrbuf.append ("world!"); One } A}
According to the above analysis, when STR is passed to STRBUF, the address of the Hello store is passed through, then the output is:
Before change, str == Hello world!
Modify the above code as follows:
1 Public classTest2 {2 Public Static voidMain (string[] args) {3StringBuffer str =NewStringBuffer ("Hello");4System.out.println ("before change, str =" +str);5 changedata (str);6System.out.println ("after changedata, str =" +str);7 }8
9 Public Static voidChangedata (StringBuffer strbuf) {TenStrbuf =NewStringBuffer ("Hi"); OneStrbuf.append ("world!"); A } -}
According to the above analysis, when STR is passed to Strbuf, both Strbuf and STR will store the Hello storage address, for example:
Then when execution is finished strbuf = new StringBuffer ("Hi"); , the above relationship will become:
At this point, the strbuf is not the address of Hello, but the address of Hi, the new operator will always be in memory after the successful opening of a storage area.
When executing strbuf.append ("world!"); At this point, the operation will be hi, not hello, then the result will be:
The final output results are as follows:
Before change, str == Hello
In Java and C + + The object is passed as a parameter differently