博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Effective Java 49 Prefer primitive types to boxed primitives
阅读量:5113 次
发布时间:2019-06-13

本文共 2338 字,大约阅读时间需要 7 分钟。

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().

       

    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.

转载于:https://www.cnblogs.com/haokaibo/p/prefer-primitive-types-to-boxed-primitives.html

你可能感兴趣的文章
poj2569
查看>>
使用mmap在内存中读写文件
查看>>
使用pygal_maps_world.i18n中数据画各大洲地图
查看>>
sql server必知多种日期函数时间格式转换
查看>>
ListView如何获取点击单元格内容
查看>>
jQuery EasyUI 的下拉选择combobox后台动态赋值
查看>>
(转)游戏引擎中三大及时光照渲染方法介绍(以unity3d为例)
查看>>
timeline时间轴进度“群英荟萃”
查看>>
python map函数用法
查看>>
ios之申请后台延时执行和做一个假后台的方法(系统进入长时间后台后,再进入前台部分功能不能实现)...
查看>>
编码命名规范
查看>>
耿丹16-1上半学期助教总结
查看>>
python if else elif statement
查看>>
网络编程
查看>>
文本隐藏(图片代替文字)
查看>>
three.map.control
查看>>
二叉树的深度
查看>>
java面试题
查看>>
提高码力专题(未完待续)
查看>>
IOS第17天(3,Quartz2D画板和画线)
查看>>