Java基础(二) 基本类型数据类型、包装类及自动拆装箱
我们知道基本数据类型包括byte, short, int, long, float, double, char, boolean,对应的包装类分别是Byte, Short, Integer, Long, Float, Double, Character, Boolean。关于基本数据类型的介绍可参考Java基础(一) 八大基本数据类型
那么为什么需要包装类?
JAVA是面向对象的语言,很多类和方法中的参数都需使用对象,但基本数据类型却不是面向对象的,这就造成了很多不便。
如:List<int> = new ArrayList<>();,就无法编译通过
为了解决该问题,我们引入了包装类,顾名思义,就是将基本类型“包装起来“,使其具备对象的性质,包括可以添加属性和方法,位于java.lang包下。
拆箱与装箱
既然有了基本数据类型和包装类,就必然存在它们之间的转换,如:
public static void main(String[] args) {
Integer a = 0;
for(int i = 0; i < 100; i++){
a += i;
}
}
将基本数据类型转为包装类的过程叫“装箱”;
将包装类转为基本数据类型的过程叫“拆箱”;
自动拆箱与自动装箱
Java为了简便拆箱与装箱的操作,提供了自动拆装箱的功能,这极大地方便了程序员们。那么到底是如何实现的呢?
上面的例子就是一个自动拆箱与装箱的过程,通过反编译工具我们得到,
public static void main(String[] args) {
Integer a = Integer.valueOf(0);
for (int i = 0; i < 100; i++) {
a = Integer.valueOf(a.intValue() + i);
}
}
我们不难发现,主要调用了两个方法,Integer.intValue() 和 Integer.valueOf( int i) 方法
查看Integer源码,我们找到了对应代码:
/**
* Returns the value of this {@code Integer} as an
* {@code int}.
*/
public int intValue() {
return value;
}
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
很明显,我们我们得出,Java帮你隐藏了内部细节。
拆箱的过程就是通过Integer 实体调用intValue()方法;
装箱的过程就是调用了 Integer.valueOf(int i) 方法,帮你直接new了一个Integer对象
那么哪些地方会进行自动拆装箱?
其实很简单
1.添加到集合中时,进行自动装箱
2.涉及到运算的时候,“加,减,乘, 除” 以及 “比较 equals,compareTo”,进行自动拆箱
注意的点
在上述的代码中,关于Integer valueOf(int i)方法中有IntegerCache类,在自动装箱的过程中有个条件判断
if (i >= IntegerCache.low && i <= IntegerCache.high)
结合注释
This method will always cache values in the range -128 to 127,
inclusive, and may cache other values outside of this range.
大意是:该方法总是缓存-128 到 127之间的值,同时针对超出这个范围的值也是可能缓存的。
那么为什么可能缓存?其实在IntegerCache源码中可以得到答案
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.
}
}
因为缓存最大值是可以配置的。这样设计有一定好处,我们可以根据应用程序的实际情况灵活地调整来提高性能。
与之类似还有:
Byte与ByteCache,缓存值范围-128到127,固定不可配置
Short与ShortCache,缓存值范围-128到127,固定不可配置
Long与LongCache,缓存值范围-128到127,固定不可配置
Character与CharacterCache,缓存值范围0到127,固定不可配置
Java基础(二) 基本类型数据类型、包装类及自动拆装箱的更多相关文章
- 【Java基础】基本类型的包装类作为参数传递是值传递还是引用传递
突然想到这个问题,然后做了下实验,下面以Integer来讲解,其他的忽略: import java.util.Iterator; /** * Created by lili on 15/9/24. * ...
- Java包装类的自动拆装箱
题目: Integer i = 42; Long l = 42l; Double d = 42.0; 下面为true的是 A.(i == l) B.(i == d) C.(l == d) D.i.eq ...
- Java面试题总结之Java基础(二)
Java面试题总结之Java基础(二) 1.写clone()方法时,通常都有一行代码,是什么? 答:super.clone(),他负责产生正确大小的空间,并逐位复制. 2.GC 是什么? 为什么要有G ...
- Java基础进阶:APi使用,Math,Arrarys,Objects工具类,自动拆装箱,字符串与基本数据类型互转,递归算法源码,冒泡排序源码实现,快排实现源码,附重难点,代码实现源码,课堂笔记,课后扩展及答案
要点摘要 Math: 类中么有构造方法,内部方法是静态的,可以直接类名.方式调用 常用: Math.abs(int a):返回参数绝对值 Math.ceil(double a):返回大于或等于参数的最 ...
- Java入土--Java基础(二)
Java基础(二) 接上一讲,我们接着来聊聊Java的一些基础知识,下一讲就会进行流程的控制. 类型转换 首先呢,是类型的转换,接上一个内容的数据类型,类型转换就是数据类型更进一步的应用. 由于Jav ...
- java基础04-数据类型扩展及面试题
java基础04-数据类型扩展及面试题讲解 public class demo02 { public static void main(String[] args){ // 一.整数拓展: 进制 二进 ...
- 【转】Java基础笔记 – 枚举类型的使用介绍和静态导入--不错
原文网址:http://www.itzhai.com/java-based-notes-introduction-and-use-of-an-enumeration-type-static-impor ...
- Java基础之枚举类型Enum的使用
Java基础之枚举类型Enum的使用 定义 public enum AccruedCleanEnum { SPREAD("1","发票"),OTHER(&quo ...
- java的数据类型、自动拆装箱、字面量
java 中的数据类型分为两大类:值类型(基本数据类型)和引用类型(复合数据类型) 值类型分为 1,整数类型(byte,short,int,long) 2,浮点类型(float,double) 3, ...
随机推荐
- Arcgis api for javascript学习笔记(4.5版本)-三维地图实现弹窗功能
1. 对于Graphic对象,在初始化Graphic对象时设置popupTemplate属性,即可实现点击Graphic时显示弹窗. <!DOCTYPE html> <html> ...
- (让你提前知道软件开发33):数据操纵语言(DML)
文章2部分 数据库SQL语言 数据操纵语言(DML) 数据操纵语言(Data Manipulation Language,DML)包含insert.delete和update语句,用于增.删.改数据. ...
- EM 12c: OMS Failed to start After restarting the Repository Database or reboot of the server
原文地址:http://blog.csdn.net/wanghui5767260/article/details/39398449 更全面,更专业 EM 12c: OMS Failed to star ...
- 制作WPF时钟之2
原文:制作WPF时钟之2 前段时间写了一篇"制作简单的WPF时钟",今天再制作了一个更漂亮的WPF时钟,目前仅完成了设计部分,准备将它制作成一个无边框窗体式的时钟. 效果图: ...
- 简明Python3教程 首页
A Byte of Python 'A Byte of Python' is a free book on programming using the Python language. It serv ...
- A Byte of Python (for Python 3.0) 下载
在线阅读:http://www.swaroopch.org/notes/Python_en:Table_of_Contents 英文版 下载地址1:http://files.swaroopch.com ...
- OpenGL(六) gluLookAt和gluPerspective函数解析
在调用gluLookAt和gluPerspective函数之前一般要先调用一下glLoadIdentity函数,先说一下这个函数是做什么的. glLoadIdentity glLoadIdentity ...
- 在React开发中遇到的问题——数组引用赋值
在React开发中遇到了一个问题: 需求是在一个选择组件中选择数据mydata数组,确定后将mydata数组返回到父组件,再次打开该选择组件时,从父组件获取之前选择的数据mydata并显示为已选择. ...
- [视频]产品营销之拍出好电子产品,Peter Belanger是如何为苹果产品拍照的
Peter Belanger –他就是那些颠覆你想象的苹果产品照片的摄影师.作为旧金山的顶级产品图片设计师的 Peter,他还拥有 eBay, Nike, Pixer 和 Square 等客户. 让我 ...
- blockchain_eth客户端安装 & geth使用 &批量转账(一)
这里是第一篇,主要讲eth客户端安装 eth官网 https://ethereum.org/ 国内有一个论坛内容挺多的,可以参考 http://ethfans.org/ eth客户端: eth客户端 ...