In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to understand Java reference types, the content is very detailed, interested friends can refer to, I hope to help you.
For the sake of image and typing convenience, the following contents refer to "variables of reference type" as pointers. So, if you have a C/C++ background, today's content should be easy for you to understand; otherwise, you may have to think more.
◆ Creating Java Advanced Questions
Suppose we write the following simple statement in the function:
StringBuffer str = new StringBuffer("Hello world");
Although this statement is simple, it actually contains the following three steps:
First, new StringBuffer ("Hello world") requests a chunk of memory in the heap and puts the StringBuffer object created in it.
Second, StringBuffer str declares a pointer. The pointer itself is stored on the stack (because the statement is written in a function) and can be used to point to an object of type StringBuffer. Or put another way, this pointer can be used to hold the address of a StringBuffer object.
***, where the equal sign (assignment symbol) associates the two, that is, the address of the memory just requested is saved as the value of str.
◆ Assignment and equality between reference objects
From the above diagram, we should understand the pointer variable and the pointer variable points to the object is what a relationship.
Again, let's look at the assignment problem. For the following statements:
StringBuffer str2 = str;
What does this assignment mean? What you're doing is copying the address of str to str2, remember, copying the address, the StringBuffer object itself isn't copied. So both pointers point to the same thing.
Here's another sketch (I'm tired of drawing these today):
Once you understand assignment, the problem of determining equality (i.e., the == operator) is simple. When we write the following statement "if (str2 == str)," we only judge whether the values of the two pointers (i.e., the addresses of the objects) are equal, not whether the contents of the objects pointed to are the same.
In fact, if two pointers have the same value, they must point to the same object (so the object content must be the same). However, two objects with the same content may have different addresses (for example, among cloned objects, the addresses are different).
◆final constant problem
The final modifier for reference-type variables is also a source of confusion. In fact, final only modifies the value of the pointer (i.e., the address saved by the pointer cannot be changed). As for the object pointed to by the pointer, whether the content can be changed or not, it doesn't matter. So, for the following statement:
final StringBuffer strConst = new StringBuffer();
You can modify the contents of the object it points to, such as:
strConst.append(" ");
However, you cannot modify its value, such as:
strConst = null;
◆ The problem of passing reference
The problem of passing arguments to reference types (in function calls) is a rather ridiculous one. Some books say it's a biography, some books say it's a quotation. Java programmers are almost neurotic. So, let's *** talk about the problem of "reference type parameter passing".
v Again, assuming that we want to print out the string we just created, we would use the following statement:
System.out.println (str); What does this statement mean? At this time, there were only two things to say.
*** Understanding: You can think that the pointer to str is passed into the function. The pointer is the value of an address. To put it bluntly, it is an integer. According to this understanding, it is the way to pass values. In other words, the parameter passes the pointer itself, so it passes the value.
The second understanding: you can think of the StringBuffer object passed in, according to this understanding, is the way to pass reference. Because we do pass in the address (i.e., reference) of the object.
After all this talk, it makes sense to pass a reference or a value, depending on how you look at what the parameter conveys. This is like the "wave-particle duality" of light in quantum mechanics. If you measure it as particles, it looks like particles; if you observe it as waves, it looks like waves. If you don't understand quantum mechanics, forget I said that before: -)
About how to understand Java reference types to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.