wrapper class (Integer 为例)
1,导入 Integer a = 100; Integer b = 100; Integer c = 150; Integer d = 150; a == b; true c == d; false
2,wrapper class(包装类) java是面向对象的语言,在java中,为了方便编程,设定了8中基本数据类型,分别是: byte,short,int,long,float,double,char,boolean 为了编程对象化操作,在java5之后,引入了包装类型,8中基本数据类型可以通过包箱,拆箱 机制和其对应的包装类进行相互的转换,基本数据类型对应的包装类分别是: Byte,Short,Integer,Long,Float,Double,Character,Boolean
3,a == b , c == d,两个对象的比较,实际是内存地址的比较。
Integer a = 100,等同于Integer a = Integer.valueOf(100);
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
当满足条件 i >= IntegerCache.low && i <= IntegerCache.high 的时候,返回缓存里的 对象,当不满条件的时候,才会创建一个新的new Integer() 对象.
4,解析,从缓存中取对象,还是创建新的对象的条件
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127; }
private IntegerCache() {}
}
IntegerCache 是Interger 的静态内部类。 IntegerCache.low : -128 IntegerCache.high : JRE可设置,不设置时候默认是127,通过属性java.lang.IntegerCache.high 来配置,从以上的代码可以看出,初始化的时候,会将统一创建值为-128 到 127 的对象放入 到Integer cache[] 中,所以赋值的时候,值若在cache的区间内,会之间从Integer.cache 中取出对象,而不用再次创建。
5,包装类的low,high 值 Byte : -128 ~ 127 Short:-128 ~ 127 Integer:-128 ~ 127 Long:-128 ~ 127 Character :0 ~ 127
6,设置包装类Integer.cache的high 值 一般来说,尽可能创建对象的时候,尽可能调用valueOf(),而不是 Integer i = new Integer(); 因为这样可以避免创建新的对象,减少内存开销,提高性能。 若想将缓存的容量扩大,(Integer:-128~127,length == 256),可通过修改VM args 来修改。
具体方法如下 (Eclipse为例)
点击Run,Run Configurations,找到Java Application,找到对应的Class,点击Arguments, 在VM Arguments 栏输入,-Djava.lang.Integer.IntegerCacheHigh = 1250,这样就将high 的值设为了1250了,也就扩大了缓存的容量。

wrapper class (Integer 为例)的更多相关文章
- 包装类和基本数据类型(以int和Integer为例)
Java的包装类就是可以直接将简单类型的变量表示为一个类.java共有六个包装类:Integer,Long,Float,Double,Character,Boolean,对应六种基本数据类型. 包装类 ...
- [转] 基于PHP Stream Wrapper开发有趣应用场景
PHP Stream Wrapper 原文:http://blog.sina.com.cn/s/blog_502c8cc40100k40e.html ,主要是基于SAE环境讲述相应的应用场景,本文经过 ...
- Java基础(35):装箱与拆箱---Java 中基本类型和包装类之间的转换(Wrapper类)
基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 JDK1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之间的转换就更加轻松便利了. 那什么是装箱 ...
- Java探秘之基本数据类型和包装类(int,Integer)(一)
最近闲来无聊打算做一个博客网,没事记记笔记什么,可是网站不好做,需要点时间,就先写写笔记来练练手. 可是要写什么呢,太难的好像我也写不出来,万一写错了误导别人就不好了. 哈哈,不多说,直奔主题,要是写 ...
- int和Integer之间的区别和联系
在工作中使用==埋下的坑这篇博文中,我们看到当使用基本类型的时候==是完全没有问题的,部分或者混合使用基本类型和装箱基本类型的时候,就可能出现问题了,那么我们可能会想基本类型和装箱基本类型 ...
- Java8 Lambda表达式和流操作如何让你的代码变慢5倍
原文出处:ImportNew 有许许多多关于 Java 8 中流效率的讨论,但根据 Alex Zhitnitsky 的测试结果显示:坚持使用传统的 Java 编程风格——iterator 和 for- ...
- el 表达式 和 ognl表达式
el (expression language) el 基础操作符 el 能够隐含对象(就是可以直接访问的) el 的两种使用方式,第二种好像在jsp中没有什么用,主要用于jsf el能够访问的对象( ...
- Java基础——常用类(Date、File)以及包装类
本文要点: 基本数据类型的包装类 字符串相关类: 不可变字符序列:String 可变字符序列:StringBuffer.StringBuilder 时间处理相关类: Date DateFormat.S ...
- Java8 Stream代码详解+BenchMark测试
Java8 Stream基础.深入.测试 1.基本介绍 1.创建方式 1.Array的Stream创建 1.直接创建 // main Stream stream = Stream.of("a ...
随机推荐
- CF1103D Professional layer dp
正解:dp 解题报告: 传送门! 首先不难想到求个gcd,然后把gcd质因数分解成p1w1*p2w2*p3w3*...*pmwm 显然只要满足对每个p有一个ai%pj!=0就好,也就是说对每个pj找出 ...
- min-max容斥笔记及例题
这个东西是一个非常好玩的数学工具. $$max(S)=\sum_{T\subset S}(-1)^{|T|-1}min(T)$$ $$max_k(S)=\sum_{T\subset S}(-1)^{| ...
- es组合多个条件进行查询
GET /test_index/_search{ "query": { "bool": { "must": { "match&qu ...
- office excel中怎么添加批注及修改批注用户名
office excel中怎么添加批注及修改批注用户名 参考:https://jingyan.baidu.com/article/c33e3f48a52853ea15cbb5db.html 1. of ...
- nessus的安装
nessus安装 .下载地址 http://www.tenable.com/products/nessus/select-your-operating-system .获取注册码 www.nessus ...
- linux服务器性能查看
1.1 cpu性能查看 1.查看物理cpu个数: cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc -l 2.查看每个物理cpu ...
- vue 上传图片 input=file
一.逻辑 点击li触发事件chooseImage 即触发input标签事件photoChange input标签事件photoChange file返回的是如下变量 模拟上传表单方法 执行上传 二.代 ...
- DS1-3
#include <stdio.h> #include <string.h> #define MAXSIZE 1024 double polynomial[MAXSIZE]; ...
- docker知识点
1 安装 http://www.runoob.com/docker/centos-docker-install.html 2 dockerhub官网找到 emqttd 执行 docker pull ...
- IOP知识点(3)-Modal.show
1.position 模态框初始位置.可设为字符串 "左位置 上位置" 或数组 [左位置, 上位置],规则如下: 左位置 可设为 left|center|right 三者之一,上位 ...