Java Novice Advanced: Refer to the type of reference (programming random blog)

Source: Internet
Author: User
Tags stringbuffer

In the previous few days of the post "Java performance optimization [1]: Basic type vs Reference type", I probably described the difference between "reference type" and "Basic type" on storage. A netizen in the comments yesterday criticized that "the reference type variable and the object it refers to" are not clearly distinguished and easily confused. So let's take a look at the details of the reference type today.
In addition, by the way, the original post, about the "two types of storage" This section added, add some illustrations, to help everyone understand.
In fact, a variable of a reference type is very similar to a C + + pointer. For the sake of image, also for the convenience of typing, later in this article, the "Reference type of variable" is called " pointer ". So, if you had a C/n + + background, what you said today should be well understood; otherwise, you might want to think more about it.

★ Create a problem

Suppose we write the following simple statement in "function":

New StringBuffer ("HelloWorld");

Although this statement is simple, it contains the following three steps:
First, the new operator applies a lump of memory to the heap and puts the created StringBuffer object in.
Second, StringBuffer str declares a pointer. The pointer itself is stored on the stack (as I said earlier: This statement is written in "function") to point to an object of type StringBuffer. Or in other words, this pointer can be used to hold the address of a StringBuffer object.
Finally, this equals sign (the assignment symbol) associates the two, which is to save the address of the lump of memory that was just applied to the value of Str.

In order to deepen the impression of yours faithfully crossing, take the picture of the Last post and show it again:

★ Reference objects between the assignment, the same sentence

Through the above diagram, you should understand what the pointer variable and the object that the pointer variable points to is a relationship?
Let's take a look at the question of assignment after the example just followed. For the following statement:

StringBuffer str2 = str;

What does this assignment mean, pinch? In fact, it is to copy the address of STR to str2; Remember, it is the copy of the address, the StringBuffer object itself and "no" copy. So the two pointers point to the same stuff.
Another one, as follows (drawing these pictures today can make me tired):


Understanding the assignment, the problem of judging equality (= = = operator) is simple. When we write the following statement, we just judge whether the "value" of two pointers (that is, the address of the object) is equal, and "not" determines whether the object being pointed to is the same content.

if (str2 = = str)

In fact, the values of the two pointers are the same, they must point to the same object (so the object content must be the same). However, two objects of the same content may have different addresses (for example, multiple objects are cloned from each other).

Problems with ★final constants

The final modifier for a reference type variable is also a confusing place for many people. In fact, final only modifies the value of the pointer (that is, the address that the limit pointer holds cannot be changed). As for the object pointed to by the pointer, whether the content can change, it is not the case. So, for the following statement:

New StringBuffer ();

You can modify the "content" of the object it points to, such as:

Strconst.append ("helloWorld");

But "cannot" modify its "value", for example:

null;

★ The issue of the transfer of parameters

The argument of the reference type (in function call) is a rather ridiculous question. Some books say it is a value, and some books say it is a quote. The Java programmer is getting into a nervous division. So, let's finally talk about the issue of "reference type parameter passing."
Take the example above, assuming that you want to print out the string of strings you just created, we'll use the following statement:

System. out. println (str);

What does this statement mean, pinch? That's the two.
The first kind of understanding:
It can be thought that the input function is the str pointer, the pointer is plainly a value of an address, said a little more white, is an integer. According to this understanding, is the way to pass the value. In other words, the parameter passes the pointer itself, so it is passed value.
The second understanding of:
It can be thought that the StringBuffer object is passed in, according to this understanding, is to pass the reference way. Because we actually pass in the address of the object (that is, the reference).
So much saliva, in fact, whether it is "citation" or "pass the value", can make sense, the key depends on you are "How to view" The parameters passed "things." This is like the "wave-and-particle duality of light" in quantum mechanics, and if you measure it as a particle, it appears to be a particle, and if you are observing it in a fluctuating way, it looks like a wave. If you don't know much about quantum mechanics, that's what I didn't say in front of:-).

Java Novice Advanced: Refer to the type of reference (programming random blog)

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.