Java 是面向对象的语言,其基本数据类型也就有了相对应的类,称为包装类。以下是基本数据类型对应的包装类:

基本数据类型

包装类

byte(1字节)

Byte

short(2字节)

Short

int(4字节)

Integer

long(8字节)

Long

float(4字节)

Float

double(8字节)

Double

char(2字节)

Character

boolean(1/8字节)

Boolean

自动装箱、拆箱:

在 jdk1.5 以前,创建 Integer 对象需要调用其构造方法:

Integer i = new Integer(5);

jdk1.5 具有自动装箱拆箱功能:

Integer i = 5;  //装箱
int j = i; //拆箱

其原理是调用了 Integer 的 valueOf(int) 和 Integer 对象的 intValue() 方法:

public static void intTest() {
Integer integer = 2;
int i = integer;
System.out.println(i);
}
//反编译后
public static void intTest() {
Integer integer = Integer.valueOf(2);
int i = integer.intValue();
System.out.println(i);
}

IntegerCache 类:

IntegerCache 是 Integer 类中一个私有的静态类,用于整型对象的缓存。

Integer i1 = 2;
Integer i2 = 2; System.out.println(i1 == i2); //true

该缓存策略仅在自动装箱时适用,也就是使用 new Integer() 的方式构建的 Integer 对象!=:

Integer i1 = new Integer(2);
Integer i2 = new Integer(2); System.out.println(i1 == i2); //false

并且只适用于整数区间 -128 到 +127:

Integer i1 = 300;
Integer i2 = 300; System.out.println(i1 == i2); //false

源码里 Integer 的 valueOf(int) 方法:

public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
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() {}
}

源码里规定了缓存的范围最小为:-128 到 +127 ,最大值映射到 java.lang.Integer.IntegerCache.high,可以使用 JVM 的启动参数 -XX:AutoBoxCacheMax=size 设置最大值。

其他包装类:

Byte,Short,Long,Character 也有相应的缓存类;

Byte,Short,Long 有固定范围: -128 到 127。对于 Character, 范围是 0 到 127;

另外,只有 Integer 可以通过参数改变范围。

boolean:

        Boolean b1 = false;
Boolean b2 = false;
Boolean b3 = true;
Boolean b4 = true; System.out.println(b1==b2); //true
System.out.println(b3==b4); //true

Integer 与 Long:

        Integer a = 1;
Integer b = 2;
Long g = 3L;
Long h = 2L; System.out.println(g==(a+b)); //true
System.out.println(g.equals(a+b)); //false
System.out.println(g.equals(a+h)); //ture

"==" 与 equals :

1,”==“可以用于原始值进行比较,也可以用于对象进行比较,当用于对象与对象之间比较时,比较的不是对象代表的值,而是检查两个对象是否是同一对象,这个比较过程中没有自动装箱发生。

2,进行对象值比较不应该使用”==“,而应该使用对象对应的 equals 方法。

3,equals 方法是 Object 类的一个方法:

     public boolean equals(Object obj) {
return (this == obj);
}

许多类会重写这个方法:

① Integer 类重载了该方法:

     public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
     Integer i1 = 300;
Integer i2 = 300; System.out.println(i1 == i2); //false 两个不同的地址引用
System.out.println(i1.equals(i2)); //true 两个相同的值(变为两个 int 之间的比较,所以比较值)

② String 重载了该方法:当两值不 == 时,比较他们的值

     public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}

所以,当对象没有重写 equals 时,== 与 equals 是等价的。

另外:

        Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2); int[] id = new int[9];

//数组下就不能这么装了...
//int[] ints = set.toArray(new Integer[0]);
Integer[] integers = set.toArray(new Integer[0]); for(int i=0;i<=set.size()-1;i++){
id[i] = set.toArray(new Integer[0])[i];
}

Java 的自动装箱拆箱的更多相关文章

  1. Java的自动装箱/拆箱

    概述 自JDK1.5开始, 引入了自动装箱/拆箱这一语法糖, 它使程序员的代码变得更加简洁, 不再需要进行显式转换.基本类型与包装类型在某些操作符的作用下, 包装类型调用valueOf()方法将原始类 ...

  2. JAVA的自动装箱拆箱

    转自:http://www.cnblogs.com/danne823/archive/2011/04/22/2025332.html 蛋呢  的空间 ??什么是自动装箱拆箱 基本数据类型的自动装箱(a ...

  3. 通过源码了解Java的自动装箱拆箱

    什么叫装箱 & 拆箱? 将int基本类型转换为Integer包装类型的过程叫做装箱,反之叫拆箱. 首先看一段代码 public static void main(String[] args) ...

  4. java自动装箱拆箱总结

    对于java1.5引入的自动装箱拆箱,之前只是知道一点点,最近在看一篇博客时发现自己对自动装箱拆箱这个特性了解的太少了,所以今天研究了下这个特性.以下是结合测试代码进行的总结. 测试代码: int a ...

  5. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法?

    参考:http://blog.csdn.net/mazhimazh/article/details/16799925 1. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法? 原始类型 ...

  6. JAVA自动装箱拆箱与常量池

    java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的 ...

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

  8. Java中的自动装箱拆箱

    Java中的自动装箱拆箱 一.自动装箱与自动拆箱 自动装箱就是将基本数据类型转换为包装类类型,自动拆箱就是将包装类类型转换为基本数据类型. 1 // 自动装箱 2 Integer total = 90 ...

  9. Java 自动装箱/拆箱

    自动装箱/拆箱大大方便了基本类型(8个基本类型)数据和它们包装类的使用 自动装箱 : 基本类型自动转为包装类(int >> Integer) 自动拆箱: 包装类自动转为基本类型(Integ ...

随机推荐

  1. vijosP1137 组合数

    vijosP1137 组合数 链接:https://vijos.org/p/1137 [思路] 唯一分解定理. 简化式子为 : C = (n*…*m) / (n-m)!. 题目要求C质因子的数目,在质 ...

  2. HW5.22

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  3. HDU-4115 Eliminate the Conflict 2sat

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4115 题意:Alice和Bob玩猜拳游戏,Alice知道Bob每次会出什么,为了游戏公平,Bob对Al ...

  4. python seq

    missing parentheses in call to print  ==> python高版本 print("") name 'raw_input' is not d ...

  5. Codeforces Round #313 (Div. 2) D.Equivalent Strings (字符串)

    感觉题意不太好懂 = =# 给两个字符串 问是否等价等价的定义(满足其中一个条件):1.两个字符串相等 2.字符串均分成两个子串,子串分别等价 因为超时加了ok函数剪枝,93ms过的. #includ ...

  6. mysql group by 用法解析(详细)

    在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的 ...

  7. jQuery对象与dom对象相互转换jQuery对象与dom对象相互转换

    转至:http://www.chinaz.com/design/2010/0309/108144.shtml 刚开始学习jQuery,可能一时会分不清楚哪些是jQuery对象,哪些是DOM对象.至于D ...

  8. 聊聊LAA(LARGE ADDRESS AWARE)

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:聊聊LAA(LARGE ADDRESS AWARE).

  9. CSS-div漂浮

    显示效果如下: 代码如下: <div style="width:100%;border-top:0px solid #999999;"> <div style=& ...

  10. Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!具体解释!)

    Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bone C ...