Typical java problems: Passing values or passing references

Source: Internet
Author: User
Typical java problems: Pass the value or pass the reference-general Linux technology-Linux programming and kernel information. The following is a detailed description. Classic problems, but they are not easy to understand. Especially for java programmers with c-basics, they are more prone to confusion. Here I try to briefly describe them.

"Java functions pass values, while java functions PASS Parameters that reference objects"

These two sentences seem to have some conflicts at first, but they are facts, which has aroused confusion for many beginners. Here I try to explain this java feature using a simple example, which may be incomplete. I hope you can complete it.


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 type of object is called mutable)
* @ Param vo1
*/
Private static void changeValue1 (ValueObject vo1 ){
Vo1.setName ("A1 ");
}

/**
* Reassigning values to vo1 in a function does not change the original values outside the function.
* @ 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 {

Public ValueObject (){}

Public ValueObject (String name, int id)
{
This. name = name;
This. id = id;
}

Private String name;
Private int 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;
}
}



Interpretation: vo1 is an object. When it is used as a function parameter, it is passed to the function as a reference value. This name is a bit strange, with reference and value, whether it is a reference or a value depends on how you understand it. If you are taking the test, the official answer is the value. But it looks like reference again. I hope from this example, you can understand the difference between java parameter passing and reference passing in C/C ++ programs. In addition, this is also the embodiment of java as one of the features of the OO language: encapsulation.

First, let's talk about the relationship between object values. For example, the following code:

ValueObject v2, v3;
V2 = new ValueObject ("C", 3); the bold part creates a data structure. Assume that the data is stored in the memory address A000 and assigned to the handle v2
V3 = new ValueObject ("D", 4); the bold part creates a data structure. Assume that the data is stored in the memory address B000 and assigned to the handle v3
V2 = v3; the purpose of this statement is to pay the handle value of the B000 address operation to the v2 handle, so that v2 and v3 operate the B000 address in the same way, which means:
1. The original address A000 pointed to by v2 is changed to the memory address without master, and will be automatically reclaimed by jvm.
2. Since v2 and v3 point to the same IP address, you can also modify v2 of v3, and vice versa.

Sort the following code, and ask interested friends to run the verification.
ValueObject v2 = new ValueObject ("C", 3 );
ValueObject v3 = new ValueObject ("D", 4 );
V2 = v3;
System. out. println ("after v2 = v3 ");
System. out. println ("v2 =" + v2.getName (); // = D
System. out. println ("v3 =" + v3.getName (); // = D
V3.setName ("C1 ");
System. out. println ("after v3 setnameTo C1 ");
System. out. println ("vo =" + v2.getName (); // = C1
System. out. println ("vo3 =" + v3.getName (); // = C1

Therefore, it can be concluded that the memory address of each instance (instance, such as vo1, v2, and v3 in java are all ValueObject instances) is unique. Once it is created, each instance can operate on this address. If there is no public void setName or other method in the ValueObject class to modify the data in the instance of this class, the program does not have any other way to modify the data in the ValueObject class instance. This is the java encapsulation feature. 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.
Related Article

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.