Java 基础巩固:装箱拆箱 你真的熟悉吗
先考两道题:
Integer a1 = 300;
Integer a2 =300;
System.out.print(a1 == a2);
Integer b1 = 1;
Integer b2 = 1;
System.out.print(a1 == a2);
答案是:
false
true
Integer a1=1的时候,java编译器会默认编译成Integer a1=Integer.valueOf(1),valueOf方法里边会查看a1的值是否在-128到127,如果在这个范围,会从IntegerCache这个缓存中去取有没有值等于1的对象,如果有的话会返回缓存里的对象,没有的话会new一个。
马丹,那人说错了 integer 在-128 到127之间是静态初始化的 直接从缓存里面取得
JDK 1.7:
JDK 1.8:
IntegerCache:
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() {}
}
自动装箱的缓存??
Long 也是:
##Double,Float 不一样:
Double:
Float:
String??
http://blog.csdn.net/u011240877/article/details/47721135
Java 基础巩固:装箱拆箱 你真的熟悉吗的更多相关文章
- 通过源码了解Java的自动装箱拆箱
什么叫装箱 & 拆箱? 将int基本类型转换为Integer包装类型的过程叫做装箱,反之叫拆箱. 首先看一段代码 public static void main(String[] args) ...
- Java中的装箱拆箱
一) 装箱与拆箱 Java中有概念是一切皆对象,因为所有的类都默认继承自Object.但是,对于数据类型是个例外,如short,int,long,float,double, byte,char,bo ...
- Java 的自动装箱拆箱
Java 是面向对象的语言,其基本数据类型也就有了相对应的类,称为包装类.以下是基本数据类型对应的包装类: 基本数据类型 包装类 byte(1字节) Byte short(2字节) Short int ...
- Java的自动装箱/拆箱
概述 自JDK1.5开始, 引入了自动装箱/拆箱这一语法糖, 它使程序员的代码变得更加简洁, 不再需要进行显式转换.基本类型与包装类型在某些操作符的作用下, 包装类型调用valueOf()方法将原始类 ...
- JAVA的自动装箱拆箱
转自:http://www.cnblogs.com/danne823/archive/2011/04/22/2025332.html 蛋呢 的空间 ??什么是自动装箱拆箱 基本数据类型的自动装箱(a ...
- java基础1.5版后新特性 自动装箱拆箱 Date SimpleDateFormat Calendar.getInstance()获得一个日历对象 抽象不要生成对象 get set add System.arrayCopy()用于集合等的扩容
8种基本数据类型的8种包装类 byte Byte short Short int Integer long Long float Float double Double char Character ...
- Java基础(35):装箱与拆箱---Java 中基本类型和包装类之间的转换(Wrapper类)
基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 JDK1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之间的转换就更加轻松便利了. 那什么是装箱 ...
- Java之集合初探(二)Iterator(迭代器),collections,打包/解包(装箱拆箱),泛型(Generic),comparable接口
Iterator(迭代器) 所有实现了Collection接口的容器都有一个iterator方法, 用来返回一个实现了Iterator接口的对象 Iterator对象称作迭代器, 用来方便的实现对容器 ...
- java自动装箱拆箱总结
对于java1.5引入的自动装箱拆箱,之前只是知道一点点,最近在看一篇博客时发现自己对自动装箱拆箱这个特性了解的太少了,所以今天研究了下这个特性.以下是结合测试代码进行的总结. 测试代码: int a ...
随机推荐
- 在windows下部署laravel项目的步骤
laravel版本:5.4 php版本:7.1以上,我用的php7.2.7 1.代码库下载laravel源码,放在你自己的运行目录下 2.配置hosts域名及 apache域名配置 3.安装compo ...
- netty handlers模式
netty的handler模式真的挺方便的,可以像插件一样随意的插入自己新增的功能而不用队系统进行大的变动. 下面我们来看一下这个模式是如何实现和运行的. 待续...
- 【English】口头禅
1. Absolutely! 毫无疑问! 2. Adorable! 可爱极了! 3. Amazing! 太神奇了! 4. Anytime! 随时吩咐! 5. Almost! 差不多了! 6. Awfu ...
- javascript获取日期的年,月,日
var date = new Date(strTime); return date.getFullYear()+"-"+(date.getMonth()+1)+"-&qu ...
- 用JDOM读取XML文件
用JDOM读取XML文件需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类.Element类等的方法读取所需的内容.IB ...
- Linux 时间修改--date -s命令
Linux 时间修改 不重启修改时区 一.修改linux的时间root使用date指令:date -s1.只修改日期,不修改时间,输入:Linux代码 1. date -s 2007-08-03 da ...
- cassandra的primary key, partition key, cluster key,
https://stackoverflow.com/questions/24949676/difference-between-partition-key-composite-key-and-clus ...
- Git/GitHub仓库管理常用的3条命令
$ git add <filename.*> $ git commit -m "<write down the modification>" $ git p ...
- Mybatis学习-1(转自 csdn- http://my.csdn.net/hupanfeng 的文章)
简介 MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBa ...
- ISA2006安装和部署基础(虚拟机非域环境)
0x00. 为了測试基于HTTP隧道的绕过ISA,必须搭建模拟环境,为了不麻烦,我们这里不配合域环境认证.本次实验利用Vmware 10.0搭建好开发环境,实现ISA2006安装和部署,同一时候设定基 ...