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 ...
随机推荐
- 经典算法——Jump Game(II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- python 使用 urllib2
使用basic auth 的3种方式 1. 设置header import urllib2 from base64 import encodestring headers = {'Content-Ty ...
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ...
- 页面找不到js方法的原因,关于EasyUI
有时EasyUI中datagride写法不正确,会导致无法加载页面上其他的js方法.datagride中的逗号是一个也不能多.一定要注意: 例如以下代码中标红的逗号就会导致后边的js不能正常加载. c ...
- 什么是SAAS模式网站?
说到“SAAS”,它的读法非常有趣,有“萨斯”,有“S.A.A.S”, 还有中文白话“啥事”的.不过,大多不熟悉的朋友第一反应可能是非典?,别误会,此“SAAS”非彼“SARS”,一字之差,但是意义完 ...
- 固态继电器SSR
s107. LH1521. CPC1035N http://e22a.com/h.bXsDYw?cv=AAOzhSfJ&sm=53e30b
- js - 模块化开发的兼容exports的套路
补充:除了第一种的套路,还可以这样使用第二种.都是用来自执行函数的.第二种的好处是,还可以返回一个true. // 常用 ;(function () {})(); // 小技巧(如果不加上!会报错,加 ...
- Git/GitHub仓库管理常用的3条命令
$ git add <filename.*> $ git commit -m "<write down the modification>" $ git p ...
- silverlight RadGridView总结系列(转载)
系列一. RadGridView常用属性总结 1.不可编辑----IsReadOnly="True". 2.不自动增加行----AutoGenerateColumns= ...
- atitit.跨语言实现备份mysql数据库 为sql文件特性 api 兼容性java c#.net php js
atitit.跨语言实现备份mysql数据库 为sql文件特性 api 兼容性java c#.net php js 1. 两个方法:: bat vs mysqldump(推荐) vs lang ...