数字(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中
基本类型 | 包装类型 |
byte | Byte |
char | Character |
short | Short |
int | Integer |
float | Float |
double | Double |
boolean | Boolean |
自动拆箱,自动装箱
自动装箱: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 数组不会自动装箱
}}