Effective Java 2 study notes Chapter 5 Generic

Article 23-29 of that chapter, focuses on the use of generic programming to the safety benefits as well as the API can be more widely used.

23: Do not use the original code in the new ecological types

Using the original eco-types, such as list, you can not determine the object inside the element in the end what type, will occur ClassCastException error, but can not be found compile and run only when they can. But also has a generic, parameterized can make the compiler to know exactly the type of list, in the formulation and security advantages of both.

There are sub-types of generic rules, List <String> is the original eco-types List of a sub-type, but it is not a parameterized type List <Object> sub-types.

Can not be any element (except null outside) into the Collection <?> C, ie c.add (a) is wrong, c.add (null) was set up

In the uncertain or do not care about the collection element type of situation, the use of unrestricted wildcard, that is, Set <?>

Set <Object> is a parameterized type that can contain any object type

List<Object> strings = new ArrayList<Object>();

Set <?> Is a wildcard that can support any one collection that can contain an unknown object type of a collection of

List<Object> objects = new ArrayList<Object>();
                List<?> s = objects;
                List<String> stings = new ArrayList<String>();
                s = stings;

Set is one of the original ecosystem types, from the generic system is unsafe, just prior to the use of Java 1.5

Two exceptions:

1. In the type of text you must use the original eco-types, not to use parameterized types, List.class, String []. Class and int.class are legitimate, but the List <String>. Class, List <?>. Class is not legal


24: elimination of non-inspected warning

  1. If you can not eliminate a warning at the same time can prove cause a warning code is type-safe, (and only in such circumstances) can use a @ SuppressWarnings ( "unchecked") annotation to prohibit this warning
  2. The scope should be as small as possible to use SuppressWarnings annotation, do not use class
  3. Not to use the @. . . The time, there are annotated to explain why it is safe to do so

25: List of priority in the array

26: to give priority to generic

The use of generic than using client-side code needed to convert the type of security.

27: to give priority to generic method

A static factory method is particularly suitable for generic technology, the benefits of generic method is similar to the generic, you can ensure that the methods used without conversion.

28: Using a restricted wildcard to enhance the flexibility of API

Limited wildcard

PECS of the principles, producer-extends (List <? Extends T> Priority List <T>), consumer-super (List <? Super T> Priority List <T>)

Do not use a wildcard as the return type

29: to give priority to the safety of heterogeneous types of containers

Map with the type as a key

Under an Effective Java 2 study notes and Chapter 6, enumeration and annotations
标签: lt, string class, arraylist, class string, instanceof, element type, gt c, java 2, generic programming, ecosystem types, generic article, article 23, wildcard, gt class, parameterized types, gt strings, ecological types, generic rules, generic system, safety benefits
分类: Java
时间: 2009-01-15


  1. Effective Java 2 study notes chapter 7 method

    Article 38: Check the validity of arguments When you write a method or constructor, it should consider what li ...
  2. Effective Java 2 study notes and Chapter 6, enumeration and annotations

    Article 30: Use enum instead of int constants Enumeration type comparison int enumeration, String enumeration, ...
  3. Effective Java 2 study notes in Chapter 8 General Programming

    Article 45: The scope of local variables to minimize The scope of local variables to make the smallest, the mo ...
  4. Java IO study notes: Concepts and Principles

    Java IO study notes: Concepts and Principles First, the concept of Java in the operation of the file is stream ...
  5. Spring in Action Study Notes - Chapter IV conquest database (change)

    Spring2.0 official version ( Spring2.0 Chinese documents (http://sprin ...
  6. Effective java study notes Chapter VIII (1)

    To minimize the scope of local variables Older programming languages (such as the C language) requires a code ...
  7. java generics Study Notes

    java generics started out from the JDK1.5, although he usually has used before, but has not been a comprehensi ...
  8. <Linux Kernel Development> Study Notes Chapter

    Linux kernel has been very interested in long to be in-depth study, because of their present level of more foo ...
  9. Java threads Study Notes (2)

    Generate the return value from the task Runnable is an independent implementation of the task, does not return ...
  10. Java threads Study Notes (3) the background thread

    The so-called background thread, is when the program runs in the background thread to provide a universal serv ...
  11. Java threads Study Notes (4) thread join

    "Java programming ideas," an example, if a thread in another thread t call t.join (), this thread wi ...
  12. Java threads Study Notes (5) Resource Sharing

    IncreaseClient held in a base, from the increase each method call, return the original value, increase method ...
  13. Java threads Study Notes (7) java is not in increasing atomic

    The following test code, obtained through a self-increasing function of the value of the latest, Set you keep ...
  14. Java threads Study Notes (8) collaboration between threads

    wait () and notifyAll () Call sleep () when the lock has not been released, called yeild () is the same. When ...
  15. Java threads Study Notes (9) the producer consumer problem

    Using multiple threads to simulate the producer consumer problem. BlockingQueue used to achieve more convenien ...
  16. <Thinking In Java> Study Notes - Chapter 21, "concurrent"

    Java's thread mechanism is preemptive, which means scheduling mechanism will periodically interrupt the thread ...
  17. Java IO Study Notes (byte stream)

    Byte stream is the most basic flow, file operations, network data transmission, etc., depends on the byte stre ...
  18. java io Study Notes (character stream)

    Character stream processing and byte streams similar, API is basically the same, that is, different units of m ...
  19. Java Enumeration Study Notes

    JDK5 introduced enumeration, enumeration is a set of constants, enumeration, we can generally adopt the follow ...