Java value transfer vs reference

Source: Internet
Author: User

Is the function call of a Java program passing values or passing parameters? This is a difficult issue. If it is unclear, it is quite error-prone: P, the most classic interpretation of this problem is that "Java functions are passed in, the parameters passed by Java functions are references to objects"

These two sentences seem to be a bit difficult at first, but the meaning is quite accurate.

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 diagram was posted on the blog a while ago, but it seems absolutely authoritative from the book "deep into Java Virtual Machine. 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 parameters, the number in the corresponding memory address is passed. Therefore, "Java functions are passed values ". 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 ".

Well, just relax. After the above discussion, it is difficult to understand why Java has different basic types and objects when passing parameters :)

Now let's take an example. Let's look at the online search to see if it is better to understand than the previous explanation?

Public class testref </P> <p >{</P> <p> Public static void main (string [] ARGs) </P> <p >{</P> <p> valueobject vo1 = new valueobject ("A", 1); </P> <p> system. out. println ("after vo1:" + vo1.getname (); // = A </P> <p> changevalue1 (vo1); </P> <p> system. out. println ("after changevalue1:" + vo1.getname (); </P> <p> // = A1, changed </P> <p> changevalue2 (vo1 ); </P> <p> system. out. println ("after changevalue2:" + vo1.getname (); </P> <p> // = A1. The value assignment in changevalue2 does not affect this field. </P> <p >}</P> <p>/** </P> <p> * It is effective to change internal data using vo1's own functions., </P> <p> * because this is an operation on the object itself </P> <p> * This object is called mutable) </P> <p> * @ Param vo1 </P> <p> */</P> <p> Private Static void changevalue1 (valueobject vo1) </P> <p >{</P> <p> vo1.setname ("A1 "); </P> <p >}</P> <p>/** </P> <p> * reassigning a value to vo1 in the function does not change the original values outside the function., this changes the reference point </P> <p> * @ Param vo1 </P> <p> */</P> <p> Private Static void changevalue2 (valueobject vo1) </P> <p >{</P> <p> vo1 = new valueobject ("B", 2); </P> <p> system. out. println ("Inside changevalue2:" + vo1.getname (); </P> <p> // = B, the result change caused by the value assignment operation is only valid within changevalue2 </P> <p >}</P> <p> class valueobject </P> <p >{</P> <p> Public valueobject () {}</P> <p> Public valueobject (string name, int ID) </P> <p >{</P> <p> This. name = Name; </P> <p> This. id = ID; </P> <p >}</P> <p> private string name; </P> <p> private int ID; </P> <p> Public int GETID () {</P> <p> return ID; </P> <p >}</P> <p> Public void setid (int id) {</P> <p> This. id = ID; </P> <p >}</P> <p> Public String getname () {</P> <p> return name; </P> <p >}</P> <p> Public void setname (string name) {</P> <p> This. name = Name; </P> <p >}< br/>

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 not have methods such as public void setname to 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 a value to it, the temporary variable in the function is directed to the new address, and 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:

Parameter passing of the reference type is also a value passing of the reference type value, which is actually the object address.

1. the Java parameter passes the value.

2. All object variables in Java are references to objects.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.