About Me

我的相片
Mr. Pigg
Taipei, Taiwan
檢視我的完整簡介

星期日, 7月 13, 2008

Effective Java - Chapter 5 Generics

  1. Dont' use raw type in new code
    - Wildcard : to provide a simple interface to client

  2. Eliminate unchecked warnings- Always use the Suppress-Warnings annotation on the smallest scope possible

  3. Prefer lists to ararys
    - Arrays check type at runtime, generics check type at compile time
    - type reified / type erasure
    - can't create generic type array => type not safe because of type erasure
    - type erasure: 不能直接 new E[], new List[], new List[] (non-reifiable type)
    因為runtime沒辦法保證Array裡面存在一致的物件

  4. Favor Generic Type
    - 盡量使用Generic Type,而不要讓Client端自己cast

  5. Favor Generic Method
    - type inference
    - for static factory method
    - recursive type bound

  6. Use bounded wildcards to increase API flexibility
    - PECS
    - 如果Signature中只有一個type parameter, 就用wildcard

  7. Consider typesafe heterogeneous containers


Help:

Class.asSubClass
Class.cast

0 意見: