No.

Primitives

Boxed Primitives

1

Have their own values

Have identities distinct from their values

2

Have only fully functional values

Have one nonfunctional value which is null

3

Time and space efficient

Time and space inefficient

Note

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

    Comparator<Integer> naturalOrder = new Comparator<Integer>() {

    public int compare(Integer first, Integer second) {

    int f = first; // Auto-unboxing

    int s = second; // Auto-unboxing

    return f < s ? -1 : (f == s ? 0 : 1); // No unboxing

    }

    };

  • When you mix primitives and boxed primitives in a single operation, the boxed primitive is auto unboxed.

    public class Unbelievable {

    static Integer i;

    public static void main(String[] args) {

    if (i == 42) // This will invoke an auto-unboxed, since the i is null so there will be a NullPointerException.

    // Fixing the program is as simple as declaring i to be an int instead of an Integer.

    System.out.println("Unbelievable");

    }

    }

  • Repeatedly boxed and unboxed will cause the observed performance degradation.

    // This program is much slower than it should be because it accidentally declares a

    // local variable (sum) to be of the boxed primitive type Long instead of the primitive type long.

    public static void main(String[] args) {

    Long sum = 0L;

    for (long i = 0; i < Integer.MAX_VALUE; i++) {

    sum += i;

    }

    System.out.println(sum);

    }

    Scenario of using boxed primitives

    • As elements, keys and values in collections since you can't put primitives in collections.
    • As type parameters in parameterized types.(eg.ThreadLocal<Integer>).
    • Making reflective method invocations(Item 53).

    Summary

    Use primitives in preference to boxed primitives whenever you have the choice. Primitive types are simpler and faster. Autoboxing reduces the verbosity, but not the danger, of using boxed primitives. When your program compares two boxed primitives with the ==operator, it does an identity comparison, which is almost certainly not what you want. When your program does mixed-type computations involving boxed and unboxed primitives, it does unboxing, and when your program does unboxing, it can throw a NullPointerException. Finally, when your program boxes primitive values, it can result in costly and unnecessary object creations.

Effective Java 49 Prefer primitive types to boxed primitives的更多相关文章

  1. Effective Java 26 Favor generic types

    Use generic types to replace the object declaration Add one or more type parameters to its declarati ...

  2. Effective Java 35 Prefer annotations to naming patterns

    Disadvantages of naming patterns Typographical errors may result in silent failures. There is no way ...

  3. Effective Java 69 Prefer concurrency utilities to wait and notify

    Principle Use the higher-level concurrency utilities instead of wait and notify for easiness. Use Co ...

  4. Effective Java 18 Prefer interfaces to abstract classes

    Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...

  5. Effective Java 25 Prefer lists to arrays

    Difference Arrays Lists 1 Covariant Invariant 2 Reified at runtime Erased at run time 3 Runtime type ...

  6. Effective Java 53 Prefer interfaces to reflection

    Disadvantage of reflection You lose all the benefits of compile-time type checking, including except ...

  7. Effective Java 68 Prefer executors and tasks to threads

    Principle The general mechanism for executing tasks is the executor service. If you think in terms o ...

  8. Effective Java 20 Prefer class hierarchies to tagged classes

    Disadvantage of tagged classes 1. Verbose (each instance has unnecessary irrelevant fields). 2. Erro ...

  9. Effective Java 46 Prefer for-each loops to traditional for loops

    Prior to release 1.5, this was the preferred idiom for iterating over a collection: // No longer the ...

随机推荐

  1. Android 学习笔记之如何使用SQLite数据库来保存数据...

    PS:最近一阵子都在为考试复习...坑爹的计算机网络,复习了3天,最后该不会的还是不会...明天还考英语...真蛋疼... 学习内容: 1.使用SQLite数据库来保存数据... SQLite:   ...

  2. ASP.NET身份验证

    Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验 证用的最多,也最灵活. Forms 验证方式对基于用户的验证 ...

  3. 给你的博客加上“Fork me on Github”彩带

    起 如今,随着Git的大热以及Github的优越性,许多知名开源项目都将源代码托管到Github上了.在Github上不仅可以托管自己的开源项目,还可以Fork人家的源代码,给自己感兴趣的项目评价(s ...

  4. javascript中怎样验证密码是否含有特殊符号、数字、大小写字母,长度是否大于6小于12

    今天在温习了一下以前学过的知识,一般常用验证密码是通过正则表达式或是通过ASCII 一.用AscII码来验证

  5. 将C1Chart数据导出到Excel

    大多数情况下,当我们说将图表导出到Excel时,意思是将Chart当成图片导出到Excel中.如果是这样,你可以参考帮助文档中保存和导出C1Chart章节. 不过,也有另一种情况,当你想把图表中的数据 ...

  6. 从苹果apns的feedback服务器获取推送失败的token

    在开发自己的苹果推送服务时候,要合理的控制ios设备的Token,而这个Token是由苹果服务器Apns产生的,就是每次app问Apns要Token,由苹果服务器产生的Token会记录到Apns里面, ...

  7. Unity中启动VS时出现"Visual Studio 2010 Shell 无效的许可证数据"的解决办法

    (感觉还是cnblog好一点,刚注册成功赶紧把baidu hi的一篇文章搬过来试试) 一直用着Visual Studio 2013给Unity写代码,安装了SQL Server 2014后,在Unit ...

  8. ACdrea 1217---Cracking' RSA(高斯消元)

    ACdrea  1217---高斯消元 Description The following problem is somehow related to the final stage of many ...

  9. lnmp+phpmyadmin配置与出现问题

    本博客归moka同学(新浪微博:moka同学)本人亲自整理,如有使用,请加链接注明出处. lnmp 安装完全后,配置phpmyadmin .其访问方式为 http://202.18.400.379/p ...

  10. 一个小笔记(7):EN_1

    For nearly ten years, the Unified Modeling Language(UML) has been the industry standard for visualiz ...