Java has only a pass value

Source: Internet
Author: User

Summary: Java passes all parameters in a way that uses pass-through values, there is no reference, and there is no pass-through address.

First we need to understand a few common nouns: objects, instances, references, variables

1) object: In fact, we often define instances of classes as objects. In Java, the object is a memory space created in the process of a new object (the stack of the system), but his return value is not the object itself, but a reference to the object, which allocates a unique space in the heap, and some people call him an address, and I understand that this is the address of the object. , it can also be thought that he is the ID of the object, but one thing must be determined: He is immutable (static) (c + + addresses can be changed by the operation, so there is a communication address).

2) Example: is the object.

3) Reference: Object in Java is not able to get directly, he is a memory space, we can only by reference to indirectly get to modify his content. An object's reference is actually a value that points to the memory of the object. Object obj = new Object (); Then the value of obj is a reference to a new Object (). The value that the variable receives is a reference.

4) Variable: can be divided into the underlying type variable and the reference type variable, the underlying type variable is the basic data type of the variable, the reference type of the variable is a reference type of the variable. object obj; then obj is a variable, and the value assigned to him is a reference, which refers specifically to the object.

5) An object can have multiple references, because an object has only one memory space, so all references to the object are the same, so changing the object through a reference can cause other references to change as well.

6) Assigning a value to a variable assigns a reference to the variable, which can be used to find the corresponding object, and the reference is the bridge between the variable and the object. Variable assignment simply points the variable to a reference, so it is not possible to change the object itself that the reference points to. First of all, it is not possible to change the address of the object in Java, then we can only change the content of this object, the content of the modification must be indirectly modified by reference, so the object can only be modified by the variable to change the contents of the objects.

The procedure is as follows:

Object obj1 = new Object ();

Assigning a value between a variable and a variable is simply passing the reference to the variable, not the variable pointing to the variable, so the assignment between variables only changes the specific direction of the reference to the variable.

Object obj2 = obj1;

Object obj3 = obj1;

Object OBJ4 = new Object ();

Changes the direction of the OBJ3 variable, but he does not affect the object he pointed to before, while Obj1 still points to the original object

Obj3 = Obj4;

At last:

Back to the point, why in Java is only the value is not transmitted? Whether it's a variable assignment or a method, it's just the same as passing a value, and that value is a reference. Because the reference is passed to other variables, other variables can also modify the corresponding object. Method is also a reason to pass parameters. In short: To modify the contents of an object, you must change the property value of the object by reference, instead of changing the content by assigning a value to the variable, and assigning a variable to it only changes the specific reference of the variable. As long as you understand, the assignment process is to change the reference object that the variable points to instead of the object's copy.

In addition, the value of the original type variable is not much to say, this is known.


Java has only a pass value

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.