`
leonzhx
  • 浏览: 802338 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 23: Don’t use raw types in new code

阅读更多

1.  Raw types behave as if all of the generic type information were erased from the type declaration. For all practical purposes, the raw type List behaves the same way as the interface type List did before generics were added to the platform.

 

2.  It pays to discover errors as soon as possible after they are made, ideally at compile time. By using raw types, you don’t discover the error till runtime, long after it has happened, and in code that is far removed from the code containing the error. Once you see the ClassCastException, you have to search through the code base looking for the method invocation that put the wrong type into the collection.

 

3.  If you use raw types, you lose all the safety and expressiveness benefits of generics.

 

4.  It was deemed critical that all of legacy codes remain legal and interoperable with new code that does use generics. It had to be legal to pass instances of parameterized types to methods that were designed for use with ordinary types, and vice versa. This requirement, known as migration compatibility, drove the decision to support raw types.

 

5.  List<String> is a subtype of the raw type List, but not of the parameterized type List<Object>. As a consequence, you lose type safety if you use a raw type like List, but not if you use a parameterized type like List<Object>

Commented By Sean:You can assign a List<String> to List and add anything to it through List, but you cann’t assign List<String> to List<Object>.

 

6.  You can’t put any element (other than null) into a Collection<?> and you can’t assume anything about the type of the objects that you get out.

 

7.  There are two minor exceptions to the rule that you should not use raw types in new code, both of which stem from the fact that generic type information is erased at runtime. First, you must use raw types in class literals. (Though it does permit array types and primitive types.) Second, it is illegal to use the instanceof operator on parameterized types other than unbounded wildcard types. The use of unbounded wildcard types in place of raw types does not affect the behavior of the instanceof operator in any way.

 

分享到:
评论

相关推荐

    Google C++ Style Guide(Google C++编程规范)高清PDF

    link ▶Don't use an #include when a forward declaration would suffice. When you include a header file you introduce a dependency that will cause your code to be recompiled whenever the header file ...

    Effective C++(第三版)

    don't try to return a reference when you must return an object. 条款22:将成员变量声明为private declare data members private. 条款23:宁以non-member、non-friend替换member函数 prefer non-member non-...

    acpi控制笔记本风扇转速

    code size, and reduces CPU stack use. (Valery Podrezov + interpreter changes in version 20051202 that eliminated namespace loading during the pass one parse.) Implemented _CID support for PCI Root ...

    Bochs - The cross platform IA-32 (x86) emulator

    Changes in 2.4.6 (February 22, 2011): Brief summary : - Support more host OS to run on: - Include win64 native binary in the release. - Fixed failures on big endian hosts. - BIOS: Support for up to...

    php.ini-development

    If you use constants in your value, and these constants belong to a ; dynamically loaded extension (either a PHP extension or a Zend extension), ; you may only use these constants *after* the line ...

Global site tag (gtag.js) - Google Analytics