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. [水煮 ASP.NET Web API2 方法论](3-2)直接式路由/属性路由

    问题 怎么样可以使用更贴近资源(Controller,Action)的方式定义路由. 解决方案 可以使用属性路由直接在资源级别声明路由.只要简单的在 Action 上使用属性路由 RouteAttri ...

  2. css 布局absolute与relative的区别

    absolute:当使用时,表示在文档流中没有实际存在位置(浮动),在不设置任何方位值时,只能按兵不动,当设置了方位值之后,会紧接着去寻找距离最近的能够将它包含住的父级元素,然后进行定位. relat ...

  3. 【.NET框架】Dapper ORM 用法—Net下无敌的ORM

    假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上Dapper这款ROM.点击下载 Dapper的优势: 1,Dapper是一个轻型的ORM类.代码就一个SqlMapper.cs文件,编 ...

  4. Winform屏幕截图保存C#代码

    代码如下: using System.Runtime.InteropServices; using System.Drawing.Imaging; [System.Runtime.InteropSer ...

  5. sns社区架构设计案例分享(二)

    源码下载地址:http://www.jinhusns.com/Products/Download/?type=xcj 五. 架构使用说明 > 缓存 > 使用说明 > (一)基础类库介 ...

  6. 004_URL 路由 - URL 路由

    在Web Form 情况下,每一个 ASPX页面既是一个文件,又是一个队请求自包含的响应.而在 MVC 情况下,请求是由控制器类中的动作方法处理的,而且与硬盘上的文件没有一对一的相互关系. ASP.N ...

  7. HTML5 Audio and JavaScript Control

    IE8 以下无效 <!DOCTYPE html> <html> <head> <meta content="text/html; charset=u ...

  8. cURL POST command line on WINDOWS RESTful service

    26down votefavorite 7 My problem: Running windows 7 and using the executable command line tool to cu ...

  9. The type java.util.Map$Entry cannot be resolved. It is indirectly referenced。。.相似的错误

    这个问题是出现一般都是因为JDK版本的问题.今天公司安装NC的时候就出现了这个问题.经过对错误的分析和猜测,将JDK从1.8i换成了1.7,之后就行了.根据我个人的猜测,可能是1.8以后就不支持Map ...

  10. 【图解】Eclipse下JRebel6.2.0热部署插件安装、破解及配置【转】

    标签: 这两天在做后台管理系统,前端框架用Bootstrap,后端用SpringMVC+Velocity.在开发过程中,经常需要对界面进行微调,调整传参等,每次更改一次java代码,就得重新部署一次, ...