Address: http://dreamhead.blogbus.com/logs/2005/05/1189478.html
This is an article that has been written more than a year ago. At that time, many Java programmers only wanted to follow new things, but they didn't even have basic concepts. In many discussions, obviously, the basic skills are not hard, so I wrote a series of articles to discuss the basic skills of Java. below is the first article promoted by this idea. But in fact, only this article is actually completed. Because it was not released in time, it was forgotten in the hard disk corner. Today, the debate on Java value transfer or reference in JavaEye reminds me of this article I have written and would like to share it with you.
Basic Java skills ?? Reference
There is such a saying that the current competition is based on the two major forces of the IT battlefield, the MS family focuses on the underlying implementation, and the Java family focuses on the system architecture. There is no evidence of this statement, but it is not hard to see from the Community power of the two major forces and the success of the book market. The other side of the incident is ignored.
Coincidentally, I am a Java programmer who likes to explore the underlying implementation. Although I do not like pure coffee, I have a different flavor.
Reference
Thinking In Java, a masterpiece of the world's Taishan Beidou, introduced "Everything is Object" In Java ". In the world of Java full of objects, reference is the root of all puzzles, and all stories start from here.
What is Reference?
If you have moved into the C/C ++ world before you enter the Java World, you will not be unfamiliar with pointers. When talking about pointers, the past experiences that were unbearable to look back suddenly came to our hearts. This is not a complaint. Let's forget the pointer pain for the moment and recall the sweetness of getting started with pointers! Remember how to explain pointers in the textbooks you read? In my impression, a pointer is an address, just like a house number. With an address, you can easily find a home without having to try your best.
C ++ came onto the stage of history, and the reference came along. Let me ask a little question. What is the difference between pointer and reference? My answer comes from More Effective C ++, a world-renowned book in C ++.
No null reference.
The reference must have an initial value.
Using reference is more efficient than using pointers. Because reference does not need to be tested.
The pointer can be assigned a value again, while the reference always points to the object it initially obtained.
Design Selection:
When you point to something you need to point to, and never point to anything else, or when you implement an operator and its syntax requires pointer fulfillment, you should select reference. Use pointers at any other time.
What does this have to do with Java?
Java beginners. Given the reference name, I don't hesitate to equate it with the reference in C ++. However, I am wrong. In Java, the reference value can be left blank as you like. Compared with the differences listed above, it is not difficult to find that if Java reference is to correspond to C/C ++, it is just a pointer in the reference coat.
Therefore, all the methods for understanding pointers in C can be copied to Java. In short, reference is an address. We can think of it as a handle, grasp it, and seize the data we want to manipulate. Just as the key to understanding C is to grasp the pointer, the key to exploring Java is reference.
A small program
I know that too much text is always confusing, so let's get some code!
Public class ReferenceTricks {
Public static void main (String [] args ){
ReferenceTricks r = new ReferenceTricks ();
// Reset integer
R. I = 0;
System. out. println ("Before changeInteger:" + r. I );
ChangeInteger (r );
System. out. println ("After changeInteger:" + r. I );
// Just for format
System. out. println ();
// Reset integer
R. I = 0;
System. out. println ("Before changeReference:" + r. I );
ChangeReference (r );
System. out. println ("After changeReference:" + r. I );
}
Private static void changeReference (ReferenceTricks r ){
R = new ReferenceTricks ();
R. I = 5;
System. out. println ("In changeReference:" + r. I );
}
Private static void changeInteger (ReferenceTricks r ){
R. I = 5;
System. out. println ("In changeInteger:" + r. I );
}
Public int I;
}
Sorry, I know it is a bad coding habit to set a field to public, just to illustrate the problem.
If you are interested in running this program, I will wait for you!
OK. Have you run it? What are the results? As expected? The following is the result of my running on my machine:
Before changeInteger: 0
In changeInteger: 5
After changeInteger: 5
Before changeReference: 0
In changeReference: 5
After changeReference: 0
Here, we are concerned with two changes ?? ChangeReference and changeInteger. From the output content, we can see that the two methods are exactly the same as those in the call, and the difference occurs after the call.
Confused explanation
Let's analyze the changeInteger behavior first.
As mentioned above, the reference in Java is an address that points to a memory space that stores information about an object. Here, we do not care about how to arrange the memory. As long as we know the address, we can find the I field of the r object, and then assign it 5. Since the content of this field has been modified, after the result is returned from the function, it is the result of the change. Therefore, after the call, the I field of the r object is still 5. Shows the memory changes before and after changeInteger calls.
Reference + -------- +
----------> | I = 0 | ----------> | I = 5 |
| -------- |
| Memory |
|
|
+ -------- ++ -------- +
After changeReferenc is called
Let's turn to changeReference.
From the code, we can see that the difference between changeInteger and changeInteger is only that there is one more sentence.
R = new ReferenceTricks ();
This statement allocates a new memory and points r to it.
After executing this statement, r is no longer the original r, but it is still a ReferenceTricks object, so we can still assign values to the I field of this r. So far, everything is so natural.
Reference + -------- ++ -------- +
----------> | I = 0 | & nb