Java 基本数据类型
基本数据类型:byte,short,int,long,char,float,double,boolean
封装数据类型:Byte,Short,Integer,Long,Character,Float,Double,Boolean
封装类型都是final修饰的
public final class Integer extends Number implements Comparable<Integer> {
封装类型内部都有一个对应的基本数据类型
private final int value;
在封装类型中有一个缓存,Integer的缓存范围是[-128,127]
private static class IntegerCache {
拆箱,装箱
https://www.cnblogs.com/dolphin0520/p/3780005.html
//创建一个新的对象
Integer x = new Integer(10);
装箱自动调用的是Integer的valueOf(int)方法
//自动装箱
Integer i = 10;//基本类型自动封装成封装类型
拆箱自动调用的是Integer的intValue方法
//自动拆箱
int a = i;//把封装类型自动编程基本类型
Integer I_1 = 1;
int i_1 = 1;
Integer I_2 = 1;
int i_2 = 1;
System.out.println(I_1 == i_1);//true
System.out.println(I_1 == I_2);//true 因为有缓存的存在
Integer I_3 = 128;
int i_3 = 128;
Integer I_4 = 128;
int i_4 = 128;
System.out.println(I_4 == i_4);//true
System.out.println(I_3 == I_4);//false 两个对象比较时,比较的但是地址
System.out.println(I_3 == i_3);//true
int i_5 = 1;
Integer I_5 = 1;
long l_5 = 1L;
Long L_5 = 1L;
System.out.println(i_5 == l_5);//true
System.out.println(i_5 == L_5);//true
System.out.println(I_5.equals(L_5));//false 因为equals中 if (obj instanceof Integer)
System.out.println(I_5.equals(i_5));//true
int i_6 = 1;
Integer I_6 = 1;
int i_7 = 2;
Integer I_7 = 2;
int i_8 = 3;
Integer I_8 = 3;
System.out.println(i_7 == i_6 + I_6);//true 出现操作符的时候会自动拆箱
System.out.println(I_7 == i_6 + I_6);//true
System.out.println(i_8 == I_6 + I_7);//true
System.out.println(I_8 == I_6 + I_7);//true
网友评论