A talk on Java Learning (II.)

Source: Internet
Author: User
Tags continue new set

In view of the last write a little bit of feelings, all encourage the younger brother to continue to write, and finally wait until the National Day Golden Week, internship finally have a rest stage, so this began to write a second article. Hope this writing is still helpful to like-minded friends. The Java dynamic loading mechanism, ClassLoader principle, and three questions about JDK and JRE were discussed. This time continue to talk about some specific class library??

1. About the collection Framework class

I believe you have learned Java of this noun is not unfamiliar to the java.util.* this package certainly is not unfamiliar. Do not know when you query the API how to examine or analyze one of the package, the most important two parts of each package is interfaces and classes, interface represents what it can do, the implementation class represents how it does. Before implementing a class, we should understand its source interface first, whether in J2SE or Java EE. So let's look at these three interfaces first: List, Set, Map. Some people may not be familiar with these three names, but believe that most people are familiar with the names of arraylist,linkedlist,treeset,hashset,hashmap,hashtable and other implementation classes. Their differences are also easy to understand, the list can be repeated collection of objects, set put in a combination of not repeatable objects, and map put <key,value > Such name value pairs, Key cannot be duplicated, value can. Here are a couple of confusing questions:

What is the difference between vector and arraylist,hashtable and hashmap?

Many interviewers like to ask this question, in fact more professional should ask: what is the difference between the new collection framework and the old collection framework? New collection Framework you can find since jdk1.2 in these packages, before vectors and Hashtable are classes included in the old collection framework. So what's the difference?

A. The naming of the new set framework is more scientific and reasonable. For example, under the list of ArrayList and LinkedList B. All of the new collection frames are not thread safe. Recommended to the JDK contained in the source code inside yourself to see the difference between vector and ArrayList it. Of course, if it is after jdk5.0 will be more ugly, because the addition of generic syntax, similar to C + + template syntax.

So, have you ever thought about why you want to lock all locks from the old collection framework by default, to prevent multiple threaded access updates to the new collection framework all unlocked, and the default way to support multithreading? (The collections static method can be used to lock up to thread-safe, of course)

The author's view is that any technology development may not follow their original intention, many major changes are affected by the objective environment. Do you know what Java's original purpose is to develop? is developed for embedded programs. Remember the last article about the ClassLoader mechanism? It is designed to save memory in the embedded development environment. Today, Java has become the language of the birth of the Internet in people's hearts. What does the Internet mean? Multithreading is the inevitable trend. The objective environment is changing, and the Java technology is becoming more and more detached from its original intention with the rapid development. It is said that the sun company is in fact the main J2SE, the result is due to the objective environmental impact, J2SE almost forgotten, stay in the focus of the discussion has been the Java EE.

The technical details here is not much to say, only use to really understand. These explanations are meant to help you understand what you are learning and what you are going to learn. Then we'll discuss it again when we talk about the Java EE.

A digression: Decades ago The IT giants were ibm,mainframe market no one can compare. How does Microsoft defeat IBM? It is because of the rapid development of hardware, the demand for personal PCs this objective environment, so that Microsoft through the OS called the second giant. Next to beat Microsoft? How does Google do that? If Microsoft does not compete with IBM for Mainframe, Google is using the Internet to rapidly develop this objective environment as a decisive factor to avoid competing with Microsoft for OS, but to take the search engine this road, called the 3rd giant. So who's the 4th giant? Many experts predict that will be in Asia or China, Whatever, the trend of objective environment change is the key to determine the direction. Of course, I also hope that will appear in China.

2. About Java Design Patterns

Many of the gof around the 23 design patterns, it seems to learn it both in school or in the workplace, has become a popular culture. I do not want to enumerate these 23 kinds of design pattern, I write these original intention always is to talk about own experience and the opinion, hoped can help everybody to understand.

First of all, I think design pattern is just a common solution to a class of problems, as long as the object-oriented programming prediction can be used in these 23 kinds. The best way to understand them is to write each one in person, even if a simple application is enough. If the code implementation can not remember, it would be a good idea to memorize their UML diagrams, assuming that UML must be understood. It is also best to use Java's own class library to help memory, such as the more commonly used observer patterns, in java.util.* There are ready-made observer interfaces and observable this implementation class, and looking at the source code is believed to be enough to understand the observer pattern. Another example is the adorner mode, you can only write a few about java.io.* program to fully understand what is the adorner mode. A lot of people think that when they get started, they shouldn't be in touch with design patterns, such as the "Java Design pattern", which is famous in the Turing Design series, the author: Steven Metsker, most of which is frankly confusing to me now. But I still disagree with the introduction and learning design patterns have any conflict, but we need to know the concept of each model and the typical application, so that we in the first time to write FileOutputStream, BufferedReader, PrintWriter can feel the original design pattern is so close to us, and not how mysterious things.

In addition, while learning some patterns, it is better to help us understand some of the characteristics of Java class Library. For example, when you are writing a prototype (Prototype) pattern, you must understand the method of java.lang.Cloneable this interface and the base class object of all classes (). The difference between deep copy and light copy: Object.clone () By default is light copy, which is copying a copy of the object, but if the object contains references to other objects and does not copy the reference, the original object and copy share the referenced object.

Deep copy is, of course, a copy of all the references that are included with the object. Both the original object and the copied object have a reference object, respectively. If you want to implement deep copy, you must first implement the Java.lang.Cloneable interface, and then override the Clone () method. Because the Clone () method in object is protected signed, the Cloneable interface's role is to magnify the protected to public so that the clone () can be overridden.

So, is there another problem? What if the referenced object references another object? Is it too much trouble to keep judging and copying it? One of my predecessors told me that the method is to rewrite the Clone method to serialize the original object directly to the disk and then deserialize it back, so that you can get a deep copy result without judgment. If you do not understand the method of serialization suggest a look at ObjectOutputStream and ObjectInputStream

In the final analysis, the model is only the ideological things, as the previous summary of the experience is actually not too much. Encourage everyone to write their own, such as the agent mode, you can simply write a child class, adult class. The child wants to buy anything to be adult by the agent. In a nutshell, the buy in adult () is actually called a child's buy (), but it is adult.buy () that is exposed to the main function. Such a simple program is enough to understand the basic meaning of the proxy model.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.