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

Item 49: Prefer primitive types to boxed primitives

阅读更多

1.  There are three major differences between primitives and boxed primitives. First, primitives have only their values, whereas boxed primitives have identities distinct from their values. Second, primitive types have only fully functional values, whereas each boxed primitive type has one nonfunctional value, which is null, in addition to all of the functional values of its corresponding primitive type. Last, primitives are generally more time- and space-efficient than boxed primitives.

 

2.  Applying the == operator to boxed primitives is almost always wrong.

 

3.  When your program does mixed-type computations involving boxed and unboxed primitives, the boxed primitive is auto-unboxed. If a null object reference is auto-unboxed, you get a NullPointerException.

 

4.  Boxed primitives have several legitimate uses. The first is as elements, keys, and values in collections. You can’t put primitives in collections, so you’re forced to use boxed primitives. This is a special case of a more general one. You must use boxed primitives as type parameters in parameterized types, because the language does not permit you to use primitives. Finally, you must use boxed primitives when making reflective method invocations.

 

5.  Use primitives in preference to boxed primitives whenever you have the choice. Primitive types are simpler and faster.

 

分享到:
评论

相关推荐

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

    Item 61: Prefer primitive types to boxed primitives Item 62: Avoid strings where other types are more appropriate Item 63: Beware the performance of string concatenation Item 64: Refer to objects by ...

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

    13. “I would prefer to spend the weekend at home rather than driving all the way to your mother’s.”说话者宁愿在家度过周末,而不愿长途驾车去对方母亲家。 14. “I should prefer beef rather than ...

    Effective C#

    **Item 22: Prefer Defining and Implementing Interfaces to Inheritance** - **Reasons:** Interfaces promote loose coupling and make it easier to swap implementations. - **Implementation:** ```csharp ...

    新人教九年级英语unit单词讲解PPT课件.pptx

    4. **特定情况**:`prefer to do sth.` 用于强调在特定情境下,更愿意做某事,例如:"I prefer to walk."(我宁愿走路。) 5. **对比形式**:`prefer doing sth. to doing sth.` 这种结构表示在两个动作之间,更...

    Copy Constructors and Assignment Operators终极解释

    在C++编程语言中,复制构造函数(Copy Constructor)和赋值运算符(Assignment Operator)是两个非常关键的概念,特别是在处理对象的拷贝和赋值时。它们默认由编译器提供,但通常需要根据具体需求进行自定义,以确保正确...

    effective c++

    2. **Item 7: Prefer Initialization to Assignment** - **核心观点**:提倡初始化而非赋值,尤其是在构造函数和成员变量的初始化过程中。 - **应用场景**:适用于对象创建时的资源管理,有助于减少内存泄漏和提高...

    九年级英语下册 Unit 6 Topic1 ABCD学案(无答案) 仁爱版

    - 宁愿…也不…:prefer…to… - 儿童节目:children's program - 新闻报道:news report - 体育节目:sports program - 教育节目:educational program - 娱乐节目:entertainment program - 爱上…:fall ...

    高考英语重点词组固定搭配大全.docx

    此外,还有一些不常接动名词但接不定式的动词,如:aim to do sth., fail to do sth., long to do sth., happen to do sth., hesitate to do sth., struggle to do sth. 二、接不定式作宾补的动词: 1. advise sb....

    ESLint 规则文件

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

    英语词汇固定搭配.doc

    - 宁愿某人做某事,例如:I prefer you to come early rather than late. 28. request sb. to do sth. - 要求某人做某事,例如:The client requested a meeting to discuss the project. 29. remind sb. to do sth...

    schemalint:Lint数据库架构

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

    安徽省太和县北城中心学校九年级英语全册Unit9IlikemusicthatIcandancetoSectionA1a_2d导学

    3. prefer sth to sth,如:I prefer fish to meat. 或 I prefer reading to playing. 通过以上内容的学习,学生应能理解并运用这些词汇和语法结构,进行关于音乐喜好的交流,同时也能理解和使用定语从句,提升英语...

    山东省龙口市诸由观镇诸由中学九年级英语全册《Unit 4 I like music that I can dance to》练习

    - 更喜欢做:prefer to do - 他最著名的一首曲子:his most famous piece - 中国民族音乐:Chinese folk music - 感觉到了强烈的伤痛:feel a strong pain - 如此… 以至于:so...that... - 查阅;查询:look ...

    【上课用】非谓语考点:动词 固定非谓语to do.docx

    - "prefer to do" 表示更倾向于做某事,而 "prefer doing" 表示平时更喜欢做某事。 题目示例: 3. 题目"If he takes on this work, he will have no choice but ____________an even greater challenge.",这里使用...

    河北专版2020中考英语复习方案第二篇语法专题突破专题09非谓语动词试题人教新目标版

    ("prefer to write emails to make phone calls") 8. 花时间做某事:spend time (in) doing sth. ("spend as much time as we can reading") 9. 使某人做某事:make sb. do sth. ("try to make Alice change ...

    介词to的用法归纳.pdf

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

    火线100天贵州专版2016中考英语总复习第一部分八上阶段测试一

    - "Many housewives prefer shopping to staying at home at weekends." "prefer doing A to doing B" 表示“宁愿做A而不愿做B”,所以C选项 "shopping;staying at home" 符合语法规则。 7. 不定式表示目的:try ...

    新目标初三英语单元检测题15套(附答案)23精选.doc

    - 宁愿去上海也不去海南:prefer to go to Shanghai rather than to Hainan 这些题目覆盖了初中英语的基础语法、词汇运用以及实际情境中的翻译能力,对于学生来说,完成这样的练习有助于他们全面提高英语能力,...

    九年级英语下册 Unit 6 Topic1学案(无答案) 仁爱版

    - 用`prefer...to...`改写:I prefer red apples to green ones. - 用`would rather...than...`改写:I would rather walk than ride a bike. - 与橘子相比,她更喜欢苹果:She prefers apples to oranges. - 我...

    九年级英语上册重点短语默写卷精选.doc

    8. 更愿意做…而不愿意做…:prefer to do…rather than do… 9. 从现在起:from now on 10. 使某人做某事:make sb. do sth. 11. 几个:several 12. 打开、关闭:open / close 13. 由…制成(看得见原料):be made ...

Global site tag (gtag.js) - Google Analytics