Advanced Java: detailed introduction to reference types

Source: Internet
Author: User

Variables of the reference type are very similar to pointers of C/C ++. For the sake of image, and for the convenience of typing, the content later in this article calls "referenced variables" a pointer. Therefore, if you have a background in C/C ++, the content you want to talk about today should be well understood. Otherwise, you may have to worry about it.

★Creation Problems

Suppose we have written the following simple statement in the function:

StringBuffer str = new StringBuffer ("Hello world ");

Although this statement is simple, it involves the following three steps:

First, new StringBuffer ("Hello world") applied for a lump of memory in the heap and put the created StringBuffer object in it.

Second, StringBuffer str declares a pointer. This pointer is stored on the stack (because the statements are written in the function) and can be used to point to a StringBuffer type object. Alternatively, this pointer can be used to save the address of a StringBuffer object.

Finally, the equal sign (Value assignment) is used to associate the two, that is, to save the address of the newly applied memory to the str value.

★Assignment and Determination of referenced objects are equal

Through the above illustration, everyone should understand the relationship between the pointer variable and the object to which the pointer variable points.

Next, let's look at the assignment problem. For the following statements:

StringBuffer str2 = str;

What does this assignment statement mean? In fact, it is to copy the str address to str2. Remember that it is address replication, and the StringBuffer object itself is not copied. So the two pointers point to the same thing.

Create another image, as shown below (I am exhausted by drawing these images today ):

Understanding the assignment and determining the equality (that is, the = Operator) is simple. When we write the following statement "if (str2 = str)", we only judge whether the values of the two pointers (that is, the object address) are equal, it does not determine whether the content of the object to be pointed to is the same.

In fact, if the two pointers have the same value, they must point to the same object (so the object content must be the same ). But the addresses of two objects with the same content may be different (for example, the addresses of the cloned objects are different ).★Final Constants

The final modifier for referenced type variables is also obfuscated by many people. In fact, final only modifies the pointer value (that is, the address saved by the pointer cannot be changed ). As for the object to which the Pointer Points, whether the content can be changed cannot be managed. Therefore, for the following statements:

Final StringBuffer strConst = new StringBuffer ();

You can modify the content of the object to which it points, for example:

StrConst. append ("");

But its value cannot be modified, for example:

StrConst = null;

★Parameter passing problems

The parameter passing problem of the reference type (in function call) is quite a drag problem. In some books, it is a value transfer, and in some books it is a reference transfer. Java programmers are almost split into neural networks. So, let's talk about the problem of "passing reference type parameters" at last.

V uses the example just now. If we want to print the string we just created, we will use the following statement:

System. out. println (str); what does this statement mean? Now let's talk about it.

First, we can think that the pointer str is passed into the function. To put it bluntly, it is an address value. To put it bluntly, it is an integer. According to this understanding, it is the method of passing values. That is to say, the parameter passes the pointer itself, so the value is passed.

The second understanding: it can be considered that the StringBuffer object is passed in. According to this understanding, it is the reference transfer method. Because we actually passed in the object address (that is, the reference.

After so much saliva is spent, whether it is to pass the reference or to pass the value, the key is how you view the parameters passed. This is like the "wave-particle two-Elephant property of light" in quantum mechanics. If you measure it as a particle, it looks like a particle; if you observe it as a fluctuation, it looks fluctuating. If you do not know much about quantum mechanics, I did not say this before :-)

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.