Java experience: Very inspiring (ii)

Source: Internet
Author: User
Tags shallow copy

We talked about the Java dynamic loading mechanism, the classloader principle, and the three questions about JDK and JRE. This continuation is to say a few specific class libraries:

1. About the collection Framework class

Believe that you have learned Java, the term is not unfamiliar to the java.util.* this package is certainly not unfamiliar. Do not know when you query the API how to examine or analyze one of the packages, each package the most important two parts is interfaces and classes, the interface represents what it can do, the implementation of the class represents how it does. Before implementing a class, we should first understand its source interface, whether in J2SE or Java EE. So let's look at these three interfaces first: List, Set, Map.

Perhaps some people are not familiar with these three names, but believe that most people are familiar with Arraylist,linkedlist,treeset,hashset,hashmap, Hashtable and other implementation of the name of the class. Their differences are also easy to understand, list can be duplicated object collection, set put non-repeatable object combination, and map put such a name value pair, key cannot be duplicated, value can. Here are a few confusing questions:

What is the difference between a vector and a arraylist,hashtable and a 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 set framework? New collection Framework you can find since jdk1.2 in these packages, before, such as vectors and Hashtable, are all classes included in the old collection framework. So what's the difference?

A. The naming of the new collection framework is more scientific and reasonable. For example, the list of ArrayList and LinkedList

B. All of the new collection framework is non-thread safe. It is recommended to go to the JDK contains the source code inside yourself to see the difference between vector and ArrayList. Of course, if it is after the jdk5.0 will be more ugly, because it added a generic syntax, similar to C + + template syntax.

So have you ever wondered why you should lock all locks from the old collection framework by default to prevent multi-threaded access from updating to the new collection framework all unlocked, the default way to support multithreading? (Of course, you can use collections static method to lock thread safety when needed)

The author's view is that any technology development is not necessarily in accordance with their original intention, many major changes are affected by the objective environment. Do you know why Java was originally developed? is developed for embedded programs. Remember the last article about the ClassLoader mechanism? That is designed to conserve memory in the embedded development environment. To this day, Java has become the language of the people's Hearts for the birth of the Internet . What does the Internet mean? Multithreading is an inevitable trend. The objective environment is changing, and Java technology is developing rapidly, which leads to more and more out of its original intention. It is said that sun is in fact the main J2SE, the result is due to the objective environmental impact, J2SE almost forgotten, stay in the focus of all the time is the Java EE.

Technical details here is not much to say, only with the ability to really understand. Explaining this is to help you understand any technology you're learning and going to learn. I'll talk about it later when we talk about the Java.

A lot of nonsense: decades ago The IT giant is IBM, mainframe market nobody comparable. How does Microsoft beat IBM? It is because of the rapid development of hardware, the demand for personal pc this objective environment, let Microsoft through the OS called the second giant. The next to beat Microsoft? Google. How do you do that? If Microsoft does not compete with IBM for Mainframe, Google, through the Internet's rapid development of the objective environment as a decisive factor, avoid with Microsoft to compete for the OS, but take the search engine this road, called the 3rd giant. So who is the 4th giant? Many expert predictions will appear in Asia or China, Whatever, the objective environment change trend is the key to decide the general direction. Of course I also hope to appear in China, ^_^ ~ ~ 2. About Java Design Patterns

Around a lot of looking at the Gof of 23 design patterns, it seems to learn it both in school and in the workplace, has become a popular trend. I do not want to enumerate these 23 design Pattern, I write these original intention has been to talk about their experiences and views, hope to help you understand.

First of all, I think the design pattern is only a general solution to a class of problems, as long as the object-oriented programming prophecy can be used in these 23 kinds. The best way to understand them is to write each one yourself, even if a simple application is enough. It would be a good idea to memorize the UML diagrams they correspond to if the code implementation doesn't remember, but it's important to understand UML.

At the same time, it is best to use Java's own class library to help memory, such as the more commonly used observer patterns, in the java.util.* have ready-made observer interface and observable this implementation class, to see the source code is believed to be enough to understand the observer pattern. For example, the adorner mode, you can just write a few about the JAVA.IO.* program to fully understand what is the adorner mode. There are a lot of people who think that when they first get into the design pattern, the "Java Design pattern", which is well known in the series of designs, Steven John Metsker, most of the examples honestly make me confused now. But I still disagree with any conflict between the introduction and the learning design pattern, but we need to know the conceptual and typical application of each pattern, so that we first write FileOutputStream, BufferedReader, PrintWriter can feel the original design pattern is so close to us, and it is not a mysterious thing.

In addition, learning certain patterns can help us understand some of the features of the Java class Library. For example, when you write a prototype (Prototype) pattern, you have to understand the java.lang.Cloneable of this interface and the base class object Clone () of all classes. That is, the difference between deep copy and shallow copy:

Object.clone () The default implementation is shallow copy, which is to copy a copy of the object, but if the object contains references to other objects, the references are not copied, so the original object and the copy share the referenced object.

Deep copy is, of course, a copy of the reference that includes the object. In this way, both the original object and the Copy object each have a reference object. 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 function of the Cloneable interface is to magnify the protected to public so that clone () can be overridden.

So, there's a problem? What if the referenced object references another object? Is it always difficult to judge and copy? One of my predecessors told me that the way to rewrite the Clone method is to serialize the original object to disk and deserialize it back, so that you can get a deep copy without judging it. If you do not understand the practice of serialization suggest a look at ObjectOutputStream and ObjectInputStream

In the final analysis, the pattern is only a matter of thought, and it is not too much for the experience of the predecessors to summarize. Encourage everyone to do their own writing, such as proxy mode, you can simply write a child class, adult class. Child wants to buy anything by adult to proxy implementation. In simple terms, the buy () inside the adult is actually called the Buy () of the child, but the adult.buy () is exposed to the main function. Such a simple program is enough to understand the basic meaning of the proxy pattern.

Java experience: Very inspiring (ii)

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.