ITeye博客

java question

standalone · 2010-11-12 11:23 · java · 编程语言 · Markdown

1. what is error? what is exception?

2. String vs. StringBuffer vs. StringBuilder

3. final vs. finally vs. finalize

4. synchronized(a){

       synchronized(b){

}

    }

    any problems?

5. way to avoid GC

6. way to avoid dead lock

7. Collection<?> vs. Collection<Object>

8. Design pattern in java library? pattern in java IO ?

评论(4)

  1. #4 楼 · standalone · 2010-11-18 13:38

    Design patterns are used and supported extensively throughout the Java APIs. Here are some examples: The Model-View-Controller design pattern is used extensively throughout the Swing API. The getInstance() method in java.util.Calendar is an example of a simple form of the Factory Method design pattern. The classes java.lang.System and java.sql.DriverManager are examples of the Singleton pattern, although they are not implemented using the approach recommended in the GoF book but with static methods. The Prototype pattern is supported in Java through the clone() method defined in class Object and the use of java.lang.Cloneable interface to grant permission for cloning. The Java Swing classes support the Command pattern by providing an Action interface and an AbstractAction class. The Java 1.1 event model is based on the observer pattern. In addition, the interface java.util.Observable and the class java.util.Observer provide support for this pattern. The Adapter pattern is used extensively by the adapter classes in java.awt.event. The Proxy pattern is used extensively in the implementation of Java's Remote Method Invocation (RMI) and Interface Definition Language (IDL) features. The structure of Component and Container classes in java.awt provide a good example of the Composite pattern. The Bridge pattern can be found in the separation of the components in java.awt (e.g., Button and List), and their counterparts in java.awt.peer. Additional examples and details on the use of design patterns in the Java APIs can be found in the following references: Matthias Felleisen and Daniel P. Friedman, A Little Java, a Few Patterns, MIT Press, 1998. Mark Grand, Patterns in Java, Volume 1, John Wiley & Sons, 1998. Mark Grand, Patterns in Java, Volume 2, John Wiley & Sons, 1999. James W. Cooper, Java Design Patterns: A Tutorial, Addison-Wesley, 2000.

  2. #3 楼 · standalone · 2010-11-16 16:04

    Consider the problem of writing a routine that prints out all the elements in a collection. Here’s how you might write it in an older version of the language: void printCollection(Collection c) { Iterator i = c.iterator(); for (k = 0; k < c.size(); k++) { System.out.println(i.next()); }} And here is a naive attempt at writing it using generics (and the new for loop syntax): void printCollection(Collection c) { for (Object e : c) { System.out.println(e); }} The problem is that this new version is much less useful than the old one. Whereas the old code could be called with any kind of collection as a parameter, the new code only takes Collection, which, as we’ve just demonstrated, is not a supertype of all kinds of collections! So what is the supertype of all kinds of collections? It’s written Collection (pronounced “collection of unknown”) , that is, a collection whose element type matches anything. It’s called a wildcard type for obvious reasons. We can write: void printCollection(Collection c) { for (Object e : c) { System.out.println(e); }} and now, we can call it with any type of collection. Notice that inside printCollection(), we can still read elements from c and give them type Object. This is always safe, since whatever the actual type of the collection, it does contain objects. It isn’t safe to add arbitrary objects to it however: Collection c = new ArrayList(); c.add(new Object()); // compile time error Since we don’t know what the element type of c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the collection. When the actual type parameter is ?, it stands for some unknown type. Any parameter we pass to add would have to be a subtype of this unknown type. Since we don’t know what type that is, we cannot pass anything in. The sole exception is null, which is a member of every type. On the other hand, given a List, we can call get() and make use of the result. The result type is an unknown type, but we always know that it is an object. It is 5 therefore safe to assign the result of get() to a variable of type Object or pass it as a parameter where the type Object is expected.

  3. #2 楼 · standalone · 2010-11-16 15:25

    explanation to java StringBuilder

    Java中的StringBuilder类功能详解 http://developer.51cto.com/art/200906/132698.htm

  4. #1 楼 · standalone · 2010-11-16 11:36

    Error: Any departure from the expected behavior of the system or program which stops the working of the system is an error. In general error is which nobody can control or guess when it occurs.

    Exception: Any error or problem which one can handle and continue to work normally. Exception can be guessed and can be handled.

    In Java Exception and Error are sub class of Throwable. It is differentiated based on the program control. Error such as OutOfMemoryError which no programmer can guess and can handle it. It depends on dynamically based on architectire OS and server configuration. Where as Exception programmer can handle it and can avoid application's misbehavior. For example if your code is looking for a file which is not available then IOException is thrown.Such instances programmer can guess and can handle it.

    Note that in Java a compile time error is normally called an "error " while a runtime error is called an "exception."

评论功能维护中,当前仅展示历史评论。