Advanced Java: detailed introduction to reference types

Source: Internet
Author: User
In the previous post, "Java performance optimization [1]: basic type vs reference type

This section describes the differences between the reference type and the basic type in storage. Yesterday, some netizens criticized in their comments that "the referenced type variable is not clearly differentiated from the object it references" and is easy to confuse. So today I want to talk about the details of the reference type. In addition, the original post
For more information about the "two types of storage methods" section, add some illustrations to help everyone understand.

In fact, 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"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

Assume thatFunction

The following simple statement is written:
Stringbuffer STR = new stringbuffer ("Hello World ");

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

First, new stringbuffer ("Hello World ")

InHeap

And put the created stringbuffer object into it.

Second, stringbuffer Str

Declares a pointer. This pointer is stored inStack

(Because the statement is written inFunction
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,Equal to No.

(Value assignment) associate the two, that is, save the address of the newly applied memory to the STR value.

Put the previous post
Take out the following picture:

 

Assignment and Determination of referenced objects are equal

Through the above illustration, everyone should understand the pointer variable and the pointer variable pointingObject

What is the relationship.

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)

", Only judge the two pointersValue

(That is, the object address) is equal, not to judgeTarget object
Whether the content 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 ("");

HoweverNo
Modify its value, 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.

Take 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 understanding:

It can be considered 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.

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.

With so much salivaUpload reference
OrPass Value

. The key is that you areView
TheThings

. 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 don't know much about quantum mechanics, I didn't say that before.


Copyright Notice

All original articles in this blog are copyrighted by the author. This statement must be reprinted to keep this article complete, and the author's programming will be noted in the form of hyperlinks
And the original address of this article:

 

Http://program-think.blogspot.com/2009/03/java-reference-types-detail.html

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.