To learn java programming well, you must pass the four levels: constructor, method overload, this keyword and garbage collection mechanism!

Some people say that you should pay attention to current affairs, finance and economics, and even popular movies and TV dramas, so that you can write explosive articles while they are hot; Some people say that you should stop writing "boring" technical articles, because the circle of programmers is really small. Even for signboards like Hongyang, the articles are so dry. How many views are there? Less than ten thousand; Some people say that you don't want to realize your knowledge in writing, because your articles are really not excellent, and I don't like to read them!

I want to say, I don't like what you say! I am also too lazy to refute, because if I don't succeed, I won't have the right to speak. It's useless to say more. I just want to be a stubborn programmer and write quietly, even if there is only one reader - sometimes, when I do something, I don't want the result, just because of the fanatical love in my heart.

Today, I'm going to talk about initialization in Java programming.

In Java programming, neither objects nor basic types are allowed to be used without initialization; Otherwise, the java compiler will enthusiastically remind you - please initialize before using. What mechanism does Java use to ensure object initialization? The answer is "constructor" -- class objects must pass the constructor before they can be initialized correctly. Program listing 1-1: a simple class with a constructor. When the keyword new is used to create an object writer, the constructor (the same method as the class name writer writer ()) will be called for initialization. Therefore, the above program will output "I am a writing enthusiast". The constructor writer () has no parameters, so it is called a parameterless constructor; In fact, a parameterless constructor can be omitted -- the compiler will automatically create a parameterless constructor, It is called "default constructor" (the Java designer is really very wise - helping programmers save the trouble of creating default constructor). Program listing 1-2: default constructor default constructor does not always "default" Yes, if a constructor has been defined, with or without parameters, the compiler will no longer automatically create the default constructor. Program listing 1-3: default constructor that will not exist all the time. Once a constructor with parameters is defined, the parameters required by the constructor must be passed when creating the object. Otherwise, the compiler will prompt "the constructor writer() is undefined" -- the advantage of this is, Ensure that the object conforms to the original intention of the class design when initializing (in the above example, the writer needs to specify the author name, so you can't create the writer object without passing the author name).

Reading Wang Xiaobo's silent majority, I like a sentence: "uneven is the source of happiness". Wang Xiaobo may mean that a society that can tolerate different views and different lifestyles is a happy society. Well, in the world of Java, there is also a happy society. Due to the particularity of the constructor (it cannot conflict with the names of other member methods). As a result, the name of the constructor must be consistent with the class name, that is, a class can only have one constructor name. This seems to limit the use of the constructor. But in fact, Java allows method overloading - there can be only one method name, but the method parameter list is different; alas, the problem is so coincidental Wonderful solution. Program listing 2-1: overloaded stem out. Println (name + stem. Out. Println (name + you see, silent Wang Er has not published books and can be a writing enthusiast; although silent Wang San has published the book "the road to advanced web stack development", he is still a good brother with Wang Er and does not despise Wang Er (never said: "Wang Er, you scum. You haven't even published a book. It's good to say you're a writing lover?"). Is it harmonious? How can Java distinguish overloaded methods (after all, the parameter names are the same)? In the above example, you can distinguish between different numbers of parameters; in addition, the type and order of parameters (not recommended because it will make the code difficult to maintain, see the following example) can also be used as the conditions for distinguishing. Program listing 2-2: method overloads that are difficult to maintain (in order, come on!)

For a long time, I avoided this keyword because I didn't understand what it was doing, I only used it in listing 2-1 (this. Name refers to the member variable of the class and name refers to the parameter of the current method). Until I met jQuery (a fast and concise JavaScript framework). Program listing 3-1: jQuery's chain calling method can be followed by another method, just like a crotch dribble followed by a backward jump shot. It's a series of actions that are supernatural, super coherent and super combustible, which makes people feel happy. What's the principle behind this? Program listing 3-2: jQuery's chain calling method After reading the program listing 3-2, you will suddenly realize that the internal of the original method returns a this, which is the reference of the current object; That is, myjq Append ("I'm a rag") show(); It is equivalent to: myjq Append ("I'm a rag"); myjq. show();。 After understanding the chain call of jQuery, let's simulate the chain call in Java (the writer goes to bed after exercise). Program listing 3-3: Java chain call

To be used correctly, objects must first be initialized, which is the beginning of everything; Then, when the object is no longer used, it needs to be cleaned up and start and finish. If you meet an interviewer who wants to "force" you to ask some questions about Java garbage collection, you can make the following preparations in advance. Q: Why garbage collection? A: If garbage collection is not carried out, the memory will be consumed and empty sooner or later. Unless the memory is infinite, we can arbitrarily allocate without recycling, but this is not the case. Therefore, garbage collection is necessary. Q: What memory needs to be recycled? A: The so-called "garbage to be recycled" is nothing more than objects that can no longer be used in any way. Q: How does Java recycle garbage? A: Garbage collection mechanism, however, in my impression, I was impressed by a funny dynamic graph, which metaphorically refers to the garbage collection mechanism of Java.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>