In Java, a variable of a reference type is very similar to a C + + pointer. This article is a beginner's step-by-step tutorial, which describes the types of references in detail.
For the sake of image, also for the convenience of typing, later in this article, the "Reference type of variable" is called a 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 Java Advanced Issues
Suppose we write the following simple statement in the function:
Although this statement is simple, it contains 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.
Second, StringBuffer str declares a pointer. The pointer itself is stored on the stack (because the statement is written in the function) and can be used 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.
Assignment between reference objects, equality of judgments
By the above diagram, you should understand what the pointer variable is to the object that the pointer variable is pointing to.
Let's take a look at the question of assignment after the example just followed. For the following statement:
What does this assignment mean, pinch? is actually to copy the address of STR to STR2, remember, is the copy of the address, the StringBuffer object itself is not copied. So the two pointers point to the same stuff.
Let's get another one, as follows (I'm tired of drawing these pictures today):
Understanding the assignment, the problem of judging equality (= = = operator) is simple. When we write the following statement "if (str2 = = str)", we simply determine whether the value of the two pointers (that is, the object's address) is equal, not whether the object being pointed to is the same as the content.
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:
Final StringBuffer strconst = new StringBuffer (); |
You can modify the contents of the object it points to, such as:
However, you cannot modify its value, for example:
Problems with 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."
V or take just the example, assuming now to print the lump of the string you just created, we will use the following statement:
System.out.println (str); What does this statement mean, pinch? That's the two.
The first 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: it can be thought that the StringBuffer object is passed in, according to this understanding, is the method of reference. Because we actually pass in the address of the object (that is, the reference).
So much saliva, in fact, whether it is a reference or a value, can make sense, the key depends on how you look at the parameters of the things passed. 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: Referring to the type of reference