Java 的自动装箱拆箱
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 的自动装箱拆箱的更多相关文章
- Java的自动装箱/拆箱
概述 自JDK1.5开始, 引入了自动装箱/拆箱这一语法糖, 它使程序员的代码变得更加简洁, 不再需要进行显式转换.基本类型与包装类型在某些操作符的作用下, 包装类型调用valueOf()方法将原始类 ...
- JAVA的自动装箱拆箱
转自:http://www.cnblogs.com/danne823/archive/2011/04/22/2025332.html 蛋呢 的空间 ??什么是自动装箱拆箱 基本数据类型的自动装箱(a ...
- 通过源码了解Java的自动装箱拆箱
什么叫装箱 & 拆箱? 将int基本类型转换为Integer包装类型的过程叫做装箱,反之叫拆箱. 首先看一段代码 public static void main(String[] args) ...
- java自动装箱拆箱总结
对于java1.5引入的自动装箱拆箱,之前只是知道一点点,最近在看一篇博客时发现自己对自动装箱拆箱这个特性了解的太少了,所以今天研究了下这个特性.以下是结合测试代码进行的总结. 测试代码: int a ...
- Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法?
参考:http://blog.csdn.net/mazhimazh/article/details/16799925 1. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法? 原始类型 ...
- JAVA自动装箱拆箱与常量池
java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的 ...
- 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中的自动装箱拆箱
Java中的自动装箱拆箱 一.自动装箱与自动拆箱 自动装箱就是将基本数据类型转换为包装类类型,自动拆箱就是将包装类类型转换为基本数据类型. 1 // 自动装箱 2 Integer total = 90 ...
- Java 自动装箱/拆箱
自动装箱/拆箱大大方便了基本类型(8个基本类型)数据和它们包装类的使用 自动装箱 : 基本类型自动转为包装类(int >> Integer) 自动拆箱: 包装类自动转为基本类型(Integ ...
随机推荐
- HW5.17
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- UVALive 5111 Soccer Teams (动态规划)
题意:给指定数量的数字“1”,“2”,“3”……,“9”.用所有这些数字加上任意个0组成一个数,要求数能被11整除,且数的位数尽量小. 能被11整除的数有一个特点,奇数位数字之和与偶数位之和的差为11 ...
- 第一章 Windows NT System Components
Page 3. The focus(焦点) of this book is Windows NT file system and the interaction(交互) of the file sys ...
- 对css中的浮动属性float刨根解牛
1.浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止. 脱离常规流,由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样. 2.几张图说明浮动常 ...
- .NET托管代码和非托管代码
.net托管代码是运行在.NET FRAMEWORK上的,类似于JAVA虚拟机托管代码:属安全代码,因为不涉及指针,但相对性能上较低,C#写出来的东西就可以认为是托管代码非托管代码:非安全的,可以使用 ...
- Spring BOOT PERFORMANCE
转自:http://www.alexecollins.com/spring-boot-performance/ 官方优化文档: https://spring.io/blog/2015/12/10/sp ...
- ThinkPad New X1 Carbon中关闭任务栏上的触摸键盘
1. 执行services.msc 2. 选择Touch Keyboard and Handwriting Panel 服务. 3. 将其停止执行. 并disable.
- PagerSlidingTabStrip
https://github.com/jpardogo/PagerSlidingTabStrip
- [Ember] Creating Your First Ember.js Project with Ember-CLI
In this lesson, we'll setup Ember-CLI and use it to create and run our first Ember.js project. Insta ...
- DW一些快捷键的使用
在键盘上敲空格的话可以使用shift+空格 如果要换行的话就可以使用的是 shift+enter