References: http://www.360doc.com/content/07/1122/09/9426_838237.shtml
In Java, only values are passed, but references are not passed. However, the passed value can be a specific value or an object reference. You can use this sentence to describe "Java functions pass values, and Java functions PASS Parameters as references to objects ".
I have seen several examples to explain this problem, but I personally feel that after reading the examples, I still only know what I don't know why, and it is quite easy to stay at the level of pictures and tigers. Therefore, it is helpful to have an understanding of the implementation principles of JVM before giving an example. The JVM structure can be found from the book deep into Java virtual machine, which is absolutely authoritative. It can be seen from the JVM structure that the JVM divides its memory into five parts during implementation, in which the program code (strictly speaking, it should be a bytecode) it is placed in the stack frame of the java stack, and the object is allocated from the heap. The heap can be understood as an "Object pool ". Objects needed in programs and programs are placed in two relatively independent regions. How do programs use objects? The answer is that the place where objects are actually used in the program is to declare an object reference, that is, to put the address of the corresponding object allocated in the heap into the reference, stack and heap are linked by reference one by one. Reference, I understand it as a pointer constant. What is a pointer constant? To put it bluntly, it is an unsigned integer, which expresses the address of the referenced object. Okay, now we know that, whether it's a basic type variable (INT, float, double or something) or an object, the corresponding memory address stores a number (unsigned integer, integer, floating Point number, etc ). When passing a parameter, it is the number in the corresponding memory address, so the "Ava function is a value transfer ". Of course, this number is different for the basic type and object type. For the basic type, this number is its value, and the result of passing the value is, changing the value of a new variable does not affect the value of the old variable. For an object, this number is its address. Passing this value is equivalent to passing a reference to a real object, the result of passing a reference or address is that the change will be globally visible, so we can say that "the parameter passed by the Java function is the reference of the object ".
After a few discussions above, it is difficult to understand why Java performs different basic types and objects when passing parameters. The following describes the specific examples to see if it is better to understand than the previous explanation?
View code
Public class testref {
Public static void main (string [] ARGs ){
Valueobject vo1 = new valueobject ("A", 1 );
System. Out. println ("after vo1:" + vo1.getname (); // =
Changevalue1 (vo1 );
System. Out. println ("after changevalue1:" + vo1.getname (); // = A1, changed
Changevalue2 (vo1 );
System. Out. println ("after changevalue2:" + vo1.getname (); // = A1, the internal value assignment of changevalue2 will not affect this.
}
/**
* It is valid to use vo1's own function to change its internal data, which can be reflected outside the function,
* This operation is performed on the object itself. This object is called mutable)
* @ Param vo1
*/
Private Static void changevalue1 (valueobject vo1 ){
Vo1.setname ("A1 ");
}
/**
* Re-assigning values to vo1 in a function does not change the original values outside the function, because this changes the reference point.
* @ Param vo1
*/
Private Static void changevalue2 (valueobject vo1 ){
Vo1 = new valueobject ("B", 2 );
System. Out. println ("Inside changevalue2:" + vo1.getname (); // = B. The result change caused by the value assignment operation is only valid within changevalue2.
}
}
Class valueobject {
Private string name;
Private int ID;
Public valueobject (){
}
Public valueobject (string name, int ID ){
This. Name = Name;
This. ID = ID;
}
Public int GETID (){
Return ID;
}
Public void setid (int id ){
This. ID = ID;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
}
In Java, the memory address of each instance (that is, the object) of an object is unique. Once it is created, the address itself can be operated. If the valueobject class does notPublic void setnameIf you modify the data in the instance of this class, the program does not have any other way to modify the data in the instance of the valueobject class. This is the encapsulation feature of Java.Classes that do not provide methods for modifying internal data are called immutable classes.Assign values to the input parameter variables in the function. You can only change the reference address pointed to by the local variable within the function range, but the content of the original address is not changed. Therefore, in changevalue2 (...) vo1 in the function has the same name as vo1 outside the function, but it is actually a different instance variable, but points to the same address as vo1 outside the function, so when we use vo1 =... when assigning values to them, the temporary variables in the function point to the new address (that is, from the original vo1 address to another address ), the content in the original vo1 memory address is not changed. This is why the value of vo1 is not modified in the main range after changevalue2 (...) is run. In changevalue1, The valueobject function is called to change its content. Therefore, the data in the original memory address is changed, so it is globally valid.
Summary: For parameter passing of the reference type, it also transmits the value of the reference type, which is actually the object address.
1. the Java parameter passes the value.
2. All object variables in Java are references to objects.
I don't know whether to explain it clearly. If I still cannot understand the principle of passing Java parameters, I strongly recommend that you read chapter 5 of "deep into Java Virtual Machine". Chinese translation is not very good. You can see the original English version, it should be better understood than the Chinese version.