Java中的包装类

数字(number)

其中包含基本类型: int double float long short byte char

但它们对比引用类型基本类型存在缺陷

缺陷:

1.无法表达不存在的值(null)值,

就是假设我开店赚钱,用一种数据类型来表达我的盈亏,假设有一天关门了,这时不能表现它的盈亏

例子:

package 数字;

public class Main {
public static void main(String[] args) {
	int[] money= {100,200,300,400,500,0,100};
	int[] money1= {100,200,null,400,500,0,100};//将空值定义为未开店——error
}
}

2.不能利用面向对象的方式去操作基本类型(比如直接用基本类型调用方法)

类似于String money;

可以调用 money.length();

但基本类型不能调用这种方法

package 数字;

public class Main {
public static void main(String[] args) {
	int age=10;
	age.double();//error
	age.stringValue();//error
}
}

3.默认情况下当方法参数是引用类型时,基本类型是不能传递的

package 数字;

public class Main {
public static void main(String[] args) {
	int age=10;
	test(age);//默认情况下是错误的这时是编译器自动转换的
}
public static void test(Object obj) {
	
}
}

解决方法:将基本类型包装成引用类型

基本类型的包装类java中已经内置了

其中,数字类型的包装类最终都继承于java.lang.Number中

基本类型包装类型
byteByte
char

Character

shortShort
intInteger
floatFloat
doubleDouble
booleanBoolean

自动拆箱,自动装箱

自动装箱:Java编译器会自动调用value方法,将基本类型转换为包装类

自动拆箱:Java编译器会自动调用xxValue方法,将包装类转换为基本类型

例:

package 数字;

public class Main {
public static void main(String[] args) {
Integer num=10;
//Integer num=Integer.valueOf(10);
Integer i1=10;
int i2=i1;
//int i2=i1.intValue();
}}

包装类的判等

推荐equals()

不推荐 != ==

package 数字;

public class Main {
public static void main(String[] args) {
	Integer i1=88;
	Integer i2=88;
	Integer i3=888;
	Integer i4=888;
	//不推荐
	System.out.println(i1==i2);//true
	System.out.println(i3==i4);//false
	//推荐
	System.out.println(i1.equals(i2));//true
	System.out.println(i3.equals(i4));//true
}}

其中equal的内部实现是在比较它们的值:

    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

i3和i4为什么不相等,因为引用类型在通过== != 比较时它们在比较时都是在比较内存地址

i1和i2为什么相等因为在Integer类中有一个静态嵌套类

  private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer[] cache;
        static Integer[] archivedCache;

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    h = Math.max(parseInt(integerCacheHighPropValue), 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            // Load IntegerCache.archivedCache from archive, if possible
            CDS.initializeFromArchive(IntegerCache.class);
            int size = (high - low) + 1;

            // Use the archived cache if it exists and is large enough
            if (archivedCache == null || size > archivedCache.length) {
                Integer[] c = new Integer[size];
                int j = low;
                for(int i = 0; i < c.length; i++) {
                    c[i] = new Integer(j++);
                }
                archivedCache = c;
            }
            cache = archivedCache;
            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

规定缓存范围在【-128,127】之内,两个88在缓存中都是同一个对象.但是888已经超过了,所以它会直接调用new Integer 返回一个新的对象。

基本类型数组与包装类数组之间是不能自动装箱,拆箱的

package 数字;

public class Main {
public static void main(String[] args) {
	int[] nums1= {11,22};
	Integerp[] nums2=num1;//error 数组不会自动装箱
}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值