I remember that there was a text named "Madman's diary" in the middle school textbook; At that time, I couldn't understand the central idea Lu Xun wanted to express in writing this article. I just felt that the full "cannibalism" was depressing; The teacher spoke impassioned on the podium. Like me, most of the students were in a daze in front of the textbook.
As an it veteran with 8 years of Java programming experience, I am ashamed to say that I have been troubled by four or five terms in Java: object, reference, heap, stack Stack (stack can be the same as stack, so it is four nouns and five nouns). Every time I see these nouns, I vaguely feel that I am slowly swallowed by an invisible big mouth, leaving only clothes debris on the ground (why not bones, because bones are easy to eat).
More than ten years later, when I read the madman's diary again, I suddenly felt like a dream:
Recently, after communicating with the friends of the silent Wang Er technology exchange group (120926808), the four or five cannibal nouns in Java: object, reference, heap, stack and stack seem to be clear in my mind. Although doubts sometimes run out when the clouds are thick - in view of this, this article just summarizes them.
In Java, although everything can be regarded as an object, what the computer operates is not the object itself, but the reference of the object. At first glance, this seems to understand rather than understand. What is an object and what is a reference? First, let's look at the definition of objects: according to popular parlance, each object is an instance of a class. Then, how to describe the instantiation process? Let's look at the code (the class is string): string ("I'm the object Zhang San" string ("I'm the object Li Si"); In Java, instantiation refers to the process of creating objects through the keyword "new". The above code will create two objects at runtime - "I am object Zhang San" and "I am object Li Si"; Now, how to operate them? We've all been to the park and met several masters. They have great skills - they can fly kites high and high, leaving us greedy! The kite flies so high that it can't fly directly by hand. It all depends on a long invisible strong rope! This is also the reason for operating Java objects. There must be a rope - that is, the "reference" to be introduced next (we often can't see it with the naked eye). = string ("I'm object Zhang San" = string ("I'm object Li Si"); How do you understand these three lines of code? Let's look at the first line of code: String Zhangsan, Lisi—— Two variables Zhangsan and Lisi are declared. Their type is string. ① ,: Zhangsan and Lisi are now referred to as references. You may have heard such an ancient saying: "God is to form, and Judah is to the sword; if you haven't heard of the sword, how can you let form die and God exist?" This is the famous saying of the atheist fan Zhen, which roughly means that the soul is to the body as the blade is to the blade; I have never heard that the blade is gone, and the blade still exists, so how can the body be allowed to die and the soul still exists? "Reference" to the object is like the blade is to the blade, and how can there be a "reference" to the object before the object is created And? If Zhangsan and Lisi cannot be called "references" at this time, what are they? The answer is very simple. It's a variable! (I understand) ②: the default values of Zhangsan and Lisi are null at this time. It should be said that the values of Zhangsan and Lisi at this time are undefined -- the keyword borrowed from JavaScript; that is, undefined; or a new keyword uninitialized -- uninitialized. However, both undefined and uninitialized are different from null. Since they are not initialized, Zhangsan and LIS I cannot be used at this time. If it is forced to use, the compiler will report an error to remind Zhangsan and Lisi that they have not been born (initialized), as shown in the figure below. If Zhangsan and Lisi are initialized to null, the compiler recognizes them (see the figure below). It can be seen that the default values of Zhangsan and Lisi are not null at this time. Let's look at the second line of code: Zhangsan = new string ("I am object Zhang San")—— Create a string object of "I am object Zhang San" and assign it to the variable Zhangsan. At this time, Zhangsan is the reference of "I am object Zhang San"; the "=" operator gives Zhangsan such sacred rights. The third line of code Lisi = new string ("I am object Li Si"); and the second line of code Zhangsan = new string ("I am object Zhang San") ; Similarly. Now, I can come to the conclusion that objects are created through the new keyword; References are object dependent= The operator assigns an object to a reference. Let's take another look at this Code: = string ("I'm object Zhang San") = string ("I'm object Li Si") = Lisi; when Zhangsan = Lisi; after execution, Zhangsan is no longer a reference to "I'm object Zhang San"; Zhangsan and Lisi point to the same object ("I'm object Li Si"); therefore, you know system.out.println (Zhangsan = = Lisi); Did you print false or true?
Who will tell me, Why are there many places (books, blogs, etc.) call the stack stack, call the stack stack stack? It makes me dizzy - I've been around the gate post for 80 times. It's no wonder I'm not dizzy! I checked Jinshan Ciba and the results are as follows: my God, I'm more dizzy. Is there any! How can I not be dizzy? I have several martial arts scripts here. You can take a look at them: 1) I'll see the stack in the future When stack and stack play cards together, directly kick out the "stack"; These three people are not suitable to play together, because pile and stack are old friends; What's the matter with your "stack" coming here; There are only "heap, stack" or "stack" (punctuation is very important). 2) heap is the space applied in memory when the program is running (it can be understood as a dynamic process); remember, it is not at compile time; therefore, the objects in Java are placed here. The advantage of this is that when an object is needed, you only need to write a line of code through the new keyword. When this line of code is executed, it will automatically distinguish the allocation space in the "heap" in memory - which is very flexible. In addition, you need to remember that the heap follows "First in, last out" rule. It's like a monk picking up a load of water and putting it in a jar. When he's thirsty, he scoops it out with a ladle. Please boldly open your brain hole and fill the process: the water at the bottom of the tank goes in first, but comes out later. Therefore, I suggest the monk put a label on the jar - the shelf life is 90 days. If you drink it after expiration, you will be responsible for the consequences! Still can't remember, look at the figure below: 3) stack, Also known as stack (it's simply not in line with the programmer's thinking. We are used to saying that one is one and two is two). It can be connected with the processor (CPU, that is, brain) is directly related, so the access speed is faster; for example, ha - the eye is close to the brain relative to the mouth, so you can see ten lines at a glance, but you can't do it. You can't read ten lines at a glance, even ten words. Since the access speed is fast, make good use of it! Java puts the reference of the object on the stack. Why? Is it because references are used frequently? No, because when compiling programs, Java must clearly know the life cycle of things stored in the stack, otherwise it will not be able to release the old memory to open up a new memory space for storing references - the space is so large that the front wave will beat the back wave to death on the beach. Now you know the heap, stack and stack?
Let's first look at a passage in Java programming thought: a series of types are often used in programming, and they need special treatment. The reason for the special treatment is that new stores objects in the "heap", so it is often not very effective to create an object with new - a very small and simple variable. Therefore, instead of creating such variables with new, create a variable that is not a reference. This variable directly stores values and is placed on the stack, so it is more efficient. In Java, these basic types are Boolean, char, byte, short, int, long, float, double and void; There are also corresponding wrappers: Boolean, character, byte, short, integer, long, float, double and void; They are involved in packing and unpacking. We have a chance to talk again. Look at two simple lines of code: a = 3, B = 3; What do these two lines of code look like when compiled? Of course, the compiler handles int a = 3 first;, Can you skip it? The compiler is processing int a = 3; Create a memory space with variable a in the stack, and then find out whether there is an address with literal value of 3. If not, open up an address with literal value of 3, and then point a to the address of 3. Compiler busy int a = 3;, Let's move on to int b = 3;; After creating the variable of B, since there is already a literal value of 3 in the stack, direct B to the address of 3; There is no need to open up new space. According to the above overview, let's assume that after defining the values of a and B, let a = 4. Is B equal to 3 or 4? Think about it and see the answer. The answer is revealed: when the compiler encounters a = 4; When, it will re search whether there is a literal value of 4 in the stack. If not, it will re open the address to store the value of 4; If it already exists, point a directly to the address 4; Therefore, the change of a value will not affect the value of B. Finally, leave a job. What will the following code output at run time? a = 1 b = 1 a = 2stem.out.println(a); System.out.println(b); TT t = TT("T"="TT"stem.out.println(t.getName()); System.out.println(t1.getName()); } } class TT{ private String name;