阅读jdk1.8源码小收获
来源:互联网 发布:ubuntu apt 安装路径 编辑:程序博客网 时间:2024/05/17 04:53
分享一个阅读源码中收获的小的优化方案。
在创建一个Boolean时,Boolean b = true;不是一个好的方案,在源码中有一个更好的 建议,可以节省时间和空间
Boolean bool = Boolean.valueOf(true);
这个是jdk源码推荐的方法
源码和注释如下:
/** * Returns a {@code Boolean} instance representing the specified * {@code boolean} value. If the specified {@code boolean} value * is {@code true}, this method returns {@code Boolean.TRUE}; * if it is {@code false}, this method returns {@code Boolean.FALSE}. * If a new {@code Boolean} instance is not required, this method * should generally be used in preference to the constructor * {@link #Boolean(boolean)}, as this method is likely to yield * significantly better space and time performance. * * @param b a boolean value. * @return a {@code Boolean} instance representing {@code b}. * @since 1.4 */public static Boolean valueOf(boolean b) { return (b ? TRUE : FALSE);}
阅读了Integer的源码 valueOf也是取的缓存中的值。
/** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #Integer(int)}, as this method is likely * to yield significantly better space and time performance by * caching frequently requested values. * * This method will always cache values in the range -128 to 127, * inclusive, and may cache other values outside of this range. * * @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i);}注意:Integer a = 3;这种情况默认走 valueOf() !!! Boolean b = true;也是默认走valueOf()方法!!
Integer c2 = new Integer(1); 官方不推荐这种建对象的方法喔
阅读全文
0 0
- 阅读jdk1.8源码小收获
- JDK1.8源码阅读系列之一:ArrayList
- Java Jdk1.8 HashMap源码阅读
- 【JDK1.6源码阅读】LinkedList
- [JDK1.7源码阅读]ArrayList
- Java Jdk1.8 HashMap源码阅读笔记一
- Java Jdk1.8 HashMap源码阅读笔记二
- JDK1.8源码阅读系列之二:LinkedList
- JDK1.8源码阅读系列之三:Vector
- JDK1.8源码阅读系列之三:Vector
- JDK1.8源码阅读系列之三:Vector
- JDK1.8源码阅读系列之三:Vector
- JDK1.8源码阅读系列之三:Vector_0
- JDK1.8源码阅读系列之三:Vector_1
- JDK1.8源码阅读系列之三:HashMap
- JDK1.8源码阅读系列之三:Vector
- 【JDK1.8】JDK1.8集合源码阅读——TreeMap(一)
- 阅读String源码总结【jdk1.6】
- NO.1 Spring IOC 先建一个简单工厂
- 修改genymotion的deviceId
- LeetCode 14 — Longest Common Prefix (Java)
- HDOJ2018 母牛的故事
- ActiveMQ的消息队列的介绍
- 阅读jdk1.8源码小收获
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence
- 我的问题
- 【剑指offer】跳台阶
- 缺陷处理流程
- JVM从入门到基础(二) 一JVM运行机制
- __proto__与prototype
- JavaScript基础(三)
- 关于eclipse的TestNG的插件安装方法