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.
相关推荐
Item 26: Don’t use raw types Item 27: Eliminate unchecked warnings Item 28: Prefer lists to arrays Item 29: Favor generic types Item 30: Favor generic methods Item 31: Use bounded wildcards to ...
// Distribute and change freely, except: don't remove my name from the source // // No warrantee of any kind, express or implied, is included with this // software; use at your own risk, ...
Like other books in this series, You Don’t Know JS: Types & Grammar dives into trickier parts of the language that many JavaScript programmers simply avoid or assume don’t exist (like types)....
You don’t need to have any prior Swift knowledge; however, object-oriented programming experience is desired. Lesson 1, Swift Basics, covers the fundamentals of using the Swift programming language....
Unit4 Don't eat in class
You.Don't.Know.JS.Types.&.Grammar.2015.1.pdf
2022最新版:DON PEPPE V1.2主题:披萨和快餐题.rar
不要让我思考(Don't Make Me Think).pdf 设计师必备。产品经理必备。网页开发者必备。
course don't load course don't load course don't load course don't load
You Don't Know JS - Types & Grammar
联邦学习相关文献
Matplotlib makes it easy to generate plots, histograms, power spectra, bar charts, error charts, and other kinds of plots, with just a few lines of code. Interactive Applications Using Matplotlib will...
As part of the "You Don't Know JS" series, this concise yet in-depth guide focuses on new asynchronous features and performance techniques—including Promises, generators, and Web Workers—that let ...
七下Unit 4 Don't eat in class测试题及答案含听力精选.doc
Don’t Make Me Think田士庆
Don’t hire a software developer until you read this book By 作者: K.N. Kukoyi ISBN-10 书号: 1539188191 ISBN-13 书号: 9781539188193 Edition 版本: 1 出版日期: 2017-05-26 pages 页数: 422 Use the ...
The 7 Day Startup: You Don't Learn Until You Launch创业必读
If getting started with this new framework sounds difficult and time-consuming, don’t worry, we’ve got you covered. ARKit by Tutorials helps you learn ARKit the quick and easy way: by following fun...
Virtualization and cloud make it easy to rapidly expand the size of infrastructure, but the habits and practices we used in the past with hardware-based infrastructure don't keep up. Teams end up with...