Java's value transfer understanding:
Code 1:
public class Test { /** * @param args */ public static void main(String[] args) { StringBuffer buffer= new StringBuffer("colin"); SChange(buffer); System.out.println( buffer); } public static void SChange (StringBuffer str) { str= new StringBuffer("huang"); } }Code 2:
public class Test { /** * @param args */ public static void main(String[] args) { StringBuffer buffer= new StringBuffer("colin"); SChange(buffer); System.out.println( buffer); } public static void SChange (StringBuffer str) { //str= new StringBuffer("huang"); str.append(" huang"); } }Let’s use two pictures to explain the above code 1 and code 2 respectively:
original state
Code 1 diagram:
Code 2 understanding:
In code 1, the copy reference points to a new object. But the original object remains unchanged.
In code 2, the copy reference changes the original object.
This is Java's passing by value.
The difference between two kinds of transfer in C++:
For C++ value passing, reference passing, and pointer methods, use the following code to understand and run the test yourself
#include <stdio.h> #include <iostream> #include <typeinfo> void ByValue(int a) { a = a + 1; } void ByRef(int& a) { a = a + 1; } void ByPointer(int* a) { *a = *a + 1; } int main(int argv, char** args) { int v = 1; ByValue(v); ByRef(v); // Pass by Reference ByPointer(&v); // Pass by Value int* vp = &v; ByPointer(vp); std::cout << v << std::endl; // std:: cout << typeid(vp).name() << std::endl; // std::cout << typeid(&vp).name() << std::endl; std::cout << "end" << std::endl; }The first one is passed by value, and the second function is passed by reference, but for the latter two, the same function is called once by Call by reference and once by Call by value.
because:
ByPointer(vp); If vp is not changed, it cannot be changed. Pass by value
ByPointer(&v); changes v. Pass by reference (you may say that what is passed is actually the address of v, and ByPointer cannot change the address of v, so this is Call by value. This sounds self-explanatory, but the address of v is pure data. When calling does not exist in the square code. For the caller, there is only v, and v is indeed changed by the ByPointer function. This result is exactly what Call by The behavior of reference. Considering behavior is the original intention of evaluation strategy. If everything is abstracted into values and the problem is considered from the perspective of data, there is no need to introduce the concept of evaluation strategy to confuse the audience.)
nob: The above understanding is recognized. The supplementary pointer method can be used in two ways. Value passing: passing a pointer; reference passing: passing the address or reference of a variable; if you use typeid(x).name() to view &v and vp, you will find that both are Point type, so two performances, the same result. You may think of me like this
ByValue(&v); //error
, while passing parameters of different types in C++ will not pass the compilation directly.
Summarize:
So I think if you pass by value or by reference, you just need to see how it goes in the memory.
Share memory means passing a reference, and copy memory means passing a value (leaving aside some special cases first)
In this case:
C/C++: Pass by value by default, pass by reference, and pointers are understood separately (pointers can be understood as passing by value or by reference, and the results are the same)
JAVA: Basic data types are passed by value, and objects are also passed by value (copy the reference of this object)
C#: Pass value by value type, pass by reference by reference type, special understanding of ref/out
Strings in JAVA and C# need special understanding. The appearance is to pass by value, but the actual implementation depends on the virtual machine.