1、基本数据类型,引用数据类型, 包装类
1.1、计算机存储单元
计算机可以用来存储数据,但无论是内存还是硬盘,计算机存储设备的最小信息单元叫“位(bit)”,我们又称为“比特位”,通常用“b”表示。而计算机中最基本的存储单元叫“字节(byte)”,通常用“B”表示。字节是连续的8个位组成。
除了字节外还有一些常用的存储单位,
1B(字节) = 8bit
1KB = 1024B
1MB = 1024KB
1GB = 1024MB
1TB = 1024GB
1.2、数据类型
Java语言是强类型语言,对于每一种数据都给出了明确的数据类型,不同的数据类型也分配了不同的内存空间,所以它们表示的数据大小也是不一样的。在Java中的数据类型包括基本数据类型和引用数据类型两种,如下图所示:
1.3、包装类与基本数据类型的区别
1.包装类是对象,拥有成员变量和成员方法。对象的调用都是通过引用对象的地址;而基本数据类型不是对象。所以包装类可以为 null,而基本数据类型不行。
2、传递方式不同
包装类是引用传递,基本类型是通过值传递的方式。
3、声明方式不同
基本数据类型不需要new关键字。包装类型需要new关键字(Integer有点特殊,见下面讲解)。
4、存储位置不同
基本数据类型将值存在栈中,包装类是把对象放在堆中,然后通过对象的引用(即指向堆中的地址(存在栈中))来调用他们。
5、使用方式不同
基本数据类型直接赋值使用就行。包装类是在集合创建对象时,遍历集合时使用。
1.4、自动拆箱与自动装箱
有了基本类型和包装类型,肯定有些时候要在它们之间进行转换。把基本类型转换成包装类型的过程叫做装箱。反之,把包装类型转换成基本类型的过程叫做拆箱。
Java 5以后, 为了减少开发人员的工作,提供了自动装箱与自动拆箱的功能。
Integer chenmo = 10; // 自动装箱
int wanger = chenmo; // 自动拆箱
等价于=>
Integer chenmo = Integer.valueOf(10);
int wanger = chenmo.intValue();
自动装箱底层是通过Integer.valueOf()完成的;自动拆箱是通过 Integer.intValue() 完成的。
当需要进行自动装箱时,如果数字在 -128 至 127 之间时,会直接使用缓存中的对象,而不是重新创建一个对象。
public class Integer111 {
public static void main(String[] args) {
// 1)基本类型和包装类型
int a = 100;
Integer b = 100;
System.out.println(a == b);//true
int a1 = -128;
Integer b1 = -128;
System.out.println(a1== b1);//true
// 2)两个包装类型
Integer a2= 127;
Integer b2 = 127;
System.out.println(a2 == b2);//true
Integer c = 128;
Integer d = 128;
System.out.println(c == d);//false
}
}
详细见下面讲解。
2、Int与Integer的关系
public class Integer111 {
public static void main(String[] args) {
Integer i1 = new Integer(12);
Integer i2 = new Integer(12);
System.out.println(i1 == i2);//false
Integer i3 = 126;
Integer i4 = 126;
int i5 = 126;
System.out.println(i3 == i4);//true
System.out.println(i3 == i5);//true
Integer i6 = 128;
Integer i7 = 128;
int i8 = 128;
System.out.println(i6 == i7);//false
System.out.println(i6 == i8);//truez(自动拆箱和装箱)
}
}
以上代码分情况来比较
1、Integer与Integer的比较:
new:一旦new,就是开辟一块新内存,结果肯定是false
不new:
看范围
Integer做了缓存,-128至127,当你取值在这个范围的时候,会采用缓存的对象,所以会相等
当不在这个范围,内部创建新的对象,此时不相等
2、Integer和int的比较:
实际比较的是数值,Integer会做拆箱的动作,来跟基本数据类型做比较,此时跟是否在缓存范围内或是否new都没关系。
源码分析:
当我们写Integer i = 126,实际上做了自动装箱:Integer i = Integer.valueOf(126);
详细见下面这段源码:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
//IntegerCache是Integer的内部类
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;
文章部分参考:
黑马程序员视频
https://zhuanlan.zhihu.com/p/58309148
https://www.jianshu.com/p/c2260088e2b8
https://blog.csdn.net/qq_34820803/article/details/87938182