java的基本数据类型及其包装类

java的数据类型

java的数据类型分为两类

  • 基本数据类型:整数int,浮点数float,字符char……。
  • 引用数据类型:类,对象,接口,数组。
  • 当我们定义基本数据类型的变量时候,其值直接保存在该变量中。
  • 如果我们定义引用数据类型的变量时候,其值所在的地址,保存在该变量中。
1.基本数据类型
1.1基本数据类型的范围

基本数据类型的范围

1.2基本数据类型之间的转换

主要有两个基本原则

  • 类型转换中默认----->容量小的可以自动转换(隐式转换)成容量大的
  • 在高级向低级转时----->强制类型转换:容量小变量=(目标数据类型)待转换变量; 在使用强制类型转换时,可能会造成损失精度。
    基本数据类型的自动转换
//当把存储范围小的值(常量值、变量的值、表达式计算的结果值)赋值给了存储范围大的变量时
int i = 'A';//char自动升级为int
double d = 10;//int自动升级为double

//当存储范围小的数据类型与存储范围大的数据类型一起混合运算时,会按照其中最大的类型运算
int i = 1;
byte b = 1;
double d = 1.0;
double sum = i + b + d;//混合运算,升级为double

//当byte,short,char数据类型进行算术运算时,按照int类型处理
byte b1 = 1;
byte b2 = 2;
byte b3 = b1 + b2;//编译报错,b1 + b2自动升级为int

short s1 = 2;
short s1 = s1 + b1;//编译报错,s1 + b2自动升级为int

char c1 = '0';
char c2 = 'A';
System.out.println(c1 + c2);//113 

注意:1.byte,short,char数据类型进行算术运算时,均会变成int

特殊的计算情况
byte b1 = 1;
int i1 = 10;
short s1 = 2;
s1 += b1; // 在使用 += ,-= ,/= ,*= , ++ ,-- 时 结果会自动进行强制类型转换
s1 += i1;
s1++;
i1 += 20/3.0; //(20/3.0)是double型
System.out.println(s1);// 14
System.out.println(i1);// 16
2.基本数据类型对应的包装类
基本数据类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

其包装类对应的是引用数据类型

3.自动装箱和自动拆箱(以int为例)

java为每一个基本数据类型对应一个包装类。
对于引用数据类型的定义声明 需要 new xxx();

//基本数据类型的声明定义
int int1 = 10//引用数据类型的声明定义
Integer integer1 = new Integer(10);

但是这样声明定义包装类太过于麻烦,于是我们可以

//自动装箱
Integer a = 10;
//自动拆箱
int b = a;
反编译后代码

自动装箱和拆箱

我们分析一下上面的自动装箱和自动拆箱

在自动装箱的时候调用了 Integer.valueof的方法
在自动拆箱时调用了Integer.intValue的方法

我们来看看这两个方法的源码

/*
这个方法可以看出自动装箱后返回的是 new Integer()
或者返回的是IntegerCache.cache[]数组中的一个对象
(IntegerCache是Integer的内部类,cache[]是存着值为-128~127的Integer对象)
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

// 自动拆箱返回的是 int值。
public int intValue() {
     return value;
 }


//Integer的内部类IntegerCache
//主要创建了一个对象数组
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() {}
    }

基本数据类型的装箱:

  • Integer,Byte,Short,Long均有缓存对象数组存在,在自动装箱时均会判断是否超过[-128,127],没有超过直接取对象数组中的对象。
  • Character 也有缓存对象数组存在,在自动装箱时均会判断是否超过 0 ~ 127 ,没有超过直接取对象数组中的对象。
public static Character valueOf(char c) {
 if (c <= 127) { // must cache
        return CharacterCache.cache[(int)c];
    }
    return new Character(c);
}
  • Boolean 比较特殊,因为他是只有true和false两个值,所以不是对象数组,而是固定的静态对象。
 /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code true}.
     */
    public static final Boolean TRUE = new Boolean(true);

    /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code false}.
     */
    public static final Boolean FALSE = new Boolean(false);
    
	public static Boolean valueOf(boolean b) {
       return (b ? TRUE : FALSE);
    }
  • Float 和 Double没有,自动装箱也是直接生成对象
  • 包装类在和基本数据类型进行比较时,包装类会自动拆箱并以基本数据类型的规则与基本数据类型进行比较
4.有趣的代码(练习练习)
 Integer aa = 127;
 Integer bb = 127;
 System.out.println(aa == bb); // true ;因为自动装箱获得的是一个对象地址
 Integer aaa = 12712; //   >127
 Integer bbb = 12712; //   >127
 System.out.println(aaa == bbb); // false ;因为自动装箱,但是其值不在[-128,127]之间,生成的是两个新对象
 Integer aInteger = new Integer(12);
 Integer bInteger = new Integer(12);
System.out.println(aInteger == bInteger); // false ; 千万别迷糊,直接new xxx();肯定生成的是两个新对象了

Integer aaInt = 12;
Integer bbInteger = new Integer(12);
System.out.println(aInt == bbInteger);//  true;bbInteger在与基本数据类型进行比较的时候,会自动拆箱并以基本数据类型的规则与基本数据类型进行比较。

Float float1 = 0F;
Float float2 = 0F;
System.out.println(float1 == float2);// false; 因为生成的是两个新对象


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值