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

Item 25: Prefer lists to arrays

阅读更多

1.  Arrays are covariant which means simply that if Sub is a subtype of Super, then the array type Sub[] is a subtype of Super[]. Generics, by contrast, are invariant: for any two distinct types Type1 and Type2, List<Type1> is neither a subtype nor a supertype of List<Type2>.

 

2.  You can’t put a String into a Long container, but with an array you find out that you’ve made a mistake at runtime; with a list, you find out at compile time.

 

3.  Arrays are reified which means that arrays know and enforce their element types at runtime. If you try to store a String into an array of Long, you’ll get an ArrayStoreException. Generics, by contrast, are implemented by erasure. This means that they enforce their type constraints only at compile time and discard (or erase) their element type information at runtime.

 

4.  It is illegal to create an array of a generic type, a parameterized type, or a type parameter: new List<E>[], new List<String>[], new E[]. If they were legal, casts generated by the compiler in an otherwise correct program could fail at runtime with a ClassCastException. For example, due to erasure, it’s allowed to put an instance of List<String> to an array of List<Integer> at runtime.

 

5.  Types such as E, List<E>, and List<String> are technically known as nonreifiable types. Intuitively speaking, a non-reifiable type is one whose runtime representation contains less information than its compile-time representation. The only parameterized types that are reifiable are unbounded wildcard types such as List<?> and Map<?,?>. It is legal to create arrays of unbounded wildcard types.

 

6.  Every time you invoke a varargs method, an array is created to hold the varargs parameters. If the element type of this array is not reifiable, you get a warning. There is little you can do about these warnings other than to suppress them.

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 28: Prefer lists to arrays Item 29: Favor generic types Item 30: Favor generic methods Item 31: Use bounded wildcards to increase API flexibility Item 32: Combine generics and varargs judiciously...

    Effective C# (Covers C# 4.0) Mar 2010

    Item 25: Implement the Event Pattern for Notifications 146 Item 26: Avoid Returning References to Internal Class Objects 154 Item 27: Prefer Making Your Types Serializable 157 Item 28: Create Large-...

    专享:prefer的用法__(全).pdf

    专享:prefer的用法__(全).pdf

    Copy Constructors and Assignment Operators终极解释

    I get asked this question sometimes from seasoned programmers who are new to C++. There are plenty of good books written on the subject, but I found no clear and concise set of rules on the Internet ...

    The DevOps 2.4 Toolkit: Continuous Deployment To Kubernetes

    Minikube and Docker for Mac or Windows should undoubtedly be there for those who prefer to "play" with Docker locally. AWS is the biggest hosting provider so Kubernetes Operations (kops) must be ...

    介词to的用法归纳.pdf

    * prefer to * compare to * contrast to 这些例句中,to表示比较或对比的意思,强调两者之间的差异或相似性。 五、tҏ 与 及 个 别 的 名 词 构 成 比 较 之 意 在这个用法中,to与及个别的名词构成比较之意。...

    ESLint 规则文件

    ESLint是针对javascript代码的静态检查工具。资源是本人配置的ESLint 规则文件。官方文档:http://eslint.org/docs/rules/。

    schemalint:Lint数据库架构

    public.actor.first_name: error prefer-text-to-varchar : Prefer text to varchar types 用法 安装方式: $ npm i -g schemalint 要运行,请确保您位于具有.schemalintrc.js配置文件的文件夹中,然后键入: $ ...

    arrays:Yii阵列助手

    安装composer require yiisoft/arrays --prefer-distArrayHelper的用法数组辅助方法是静态的,因此用法如下所示: $ username = \ Yiisoft \ Arrays \ ArrayHelper :: getValue ( $ _POST , 'username' );...

    prefer.js:HTTP首选标头中间件

    var prefer = req.prefer; if (prefer.handling === 'strict') { // strict handling } else if (prefer.handling === 'lenient') { // lenient handling } if (prefer.respondAsync) { // client prefers ...

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

    Therefore, we prefer to minimize includes, particularly includes of header files in other header files. You can significantly minimize the number of header files you need to include in your own ...

    tcping查看端口及IP链路情况

    --tee : mirror output to a filename specified after '--tee' -4 : prefer ipv4 -6 : prefer ipv6 -c : only show an output line on changed state --file : treat the "server-address" as a filename ...

    python实现歌声伴奏分离实验与开发

    Prefer recent versions of the above packages, in order to avoid compatibility issues, notably for Matplotlib. Note that this latter package is not necessary for the program to run, although you might ...

    SourceInsight30

    SI3US-721119-13700 psh create the following serial number : prefer number 6 SI3US-000006-39407

    ★后面接doing或(to-)do形式的重要句型(适合八上期末复习).docx

    例如:I prefer playing football to playing basketball. 我更喜欢踢足球,而不是篮球。 三、既可以接+sb.+doing 又可以接+sb.+do 的情况 有一些动词既可以接+sb.+doing,也可以接+sb.+do,例如: * listen to ...

    英语各种介词的用法.doc

    在动词"prefer"的使用中,"to"也有特殊规则:"prefer A to B"表示更喜欢A而不是B,但如果"prefer"后面跟的是不定式,那么表示比较的"to"应变为"rather than"。例如:"The undaunted soldier preferred death to ...

    英语词组大全5.doc

    25. persuade sb. to do sth.:说服某人做某事 26. prefer sb. to do sth.:宁愿某人做某事 27. request sb. to do sth.:要求某人做某事 28. remind sb. to do sth.:提醒某人做某事 29. teach sb. to do sth.:教...

    前端开源库-coffeelint-prefer-double-quotes

    `coffeelint-prefer-double-quotes` 是一个针对CoffeeScript语言的开源库,它旨在帮助开发者遵循特定的编码规范,特别是倾向于使用双引号的规则。CoffeeScript是一种简洁、富有表达力的JavaScript方言,它在语法上...

    prefer-safe-http-header:Chrome扩展程序启用首选

    标题中的“prefer-safe-http-header”指的是一个Chrome浏览器的扩展程序,它的主要功能是启用“Prefer-Safe”HTTP头部。这个头部是一个提议的安全增强特性,用于向服务器表明客户端(这里是Chrome浏览器)更倾向于...

    Laravel开发-laravel-pdf-to-image

    在本文中,我们将深入探讨如何在 Laravel 框架中实现 PDF 文件到图像的转换,主要基于提供的项目 "laravel-pdf-to-image"。Laravel 是一个流行且强大的 PHP 框架,它提供了丰富的功能来简化 web 开发,而这个项目则...

Global site tag (gtag.js) - Google Analytics