Integer和int的区别
- Integer是int提供的封装类,而int是Java的基本数据类型;
- Integer默认值是null,而int默认值是0;
- 声明为Integer的变量需要实例化,而声明为int的变量不需要实例化;
- Integer是对象,用一个引用指向这个对象,而int是基本类型,直接存储数值。
除了知道Integer和int最基本的区别外,还需要了解一点其他关于Integer和int实际使用中的回遇到的问题。
否则如果面试官再问一下Integer i = 1;int ii = 1; i==ii为true还是为false?估计就有一部分人答不出来了,如果再问一下其他的,估计更多的人会头脑一片混乱。所以我对它们进行了总结,希望对大家有帮助。
代码如下:
public class Main {
public static void main(String[] args) {
int a = 128;
Integer b = 127;
Integer c = 127;
Integer d = 128;
Integer e = 128;
Integer f = new Integer("128");
Integer g = new Integer("127");
Integer h = new Integer("127");
//方案一()
System.out.println("方案一:Integer与new Integer不同初始化方法的值的比较,Integer与new Integer不会相等");
//Integer e = 128;
//Integer f = new Integer("128");
if (e == f){
System.out.println("true");
}else{
System.out.println("false");
}
System.out.println("---------------------");
//方案二
System.out.println("方案二:创建两个都是直接赋值的Integer");
//Integer b = 127;
//Integer c = 127;
if (b == c){
System.out.println("true");
}else{
System.out.println("false");
}
//Integer d = 128;
//Integer e = 128;
if (d == e){
System.out.println("true");
}else{
System.out.println("false");
}
System.out.println("---------------------");
//方案三
System.out.println("方案三:两个都是new出来的Integer");
//Integer g = new Integer("127");
//Integer h = new Integer("127");
if (g == h){
System.out.println("true");
}else{
System.out.println("false");
}
System.out.println("---------------------");
//方案四
System.out.println("方案四:int与Integer比较");
//int a = 128;
//Integer f = new Integer("128");
if (a == f){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
运行结果如下:
方案一:Integer与new Integer不同初始化方法的值的比较,Integer与new Integer不会相等
false
---------------------
方案二:创建两个都是直接赋值的Integer
true
false
---------------------
方案三:两个都是new出来的Integer
false
---------------------
方案四:int与Integer比较
true
总结如下:
方案一:
我先对e、f这两个对象,使用Integer和new Integer这两个不同的初始化方法,在对这两个对象进行比较;
Integer e = 128;
Integer f = new Integer("128");
if (e == f){
System.out.println("true");
}else{
System.out.println("false");
}
返回结果:false
得出结论:Integer与new Integer不会相等。e的引用指向堆,而f指向专门存放他的内存(常量池),他们的内存地址不一样,所以为false
方案二:
创建两个都是直接赋值的Integer,在对这两个对象进行比较;
//Integer b = 127;
//Integer c = 127;
if (b == c){
System.out.println("true");
}else{
System.out.println("false");
}
//Integer d = 128;
//Integer e = 128;
if (d == e){
System.out.println("true");
}else{
System.out.println("false");
}
返回结果:
true
false
得出结论:两个都是直接赋值的Integer,如果数在-128到127之间,则是true,否则为false
原因: java在编译Integer i2 = 128的时候,被翻译成-> Integer i2 = Integer.valueOf(128);而valueOf()函数会对-128到127之间的数进行缓存
看一下源码大家都会明白,对于-128到127之间的数,会进行缓存,Integer b = 127时,会将127进行缓存,下次再写Integer c = 127时,就会直接从缓存中取,就不会new了。
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
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() {}
}
方案三:
我先对g、h这两个对象,使用new Integer初始化方法,在对这两个对象进行比较;
Integer g = new Integer("127");
Integer h = new Integer("127");
if (g == h){
System.out.println("true");
}else{
System.out.println("false");
}
返回结果:false
得出结论:两个都是new出来的Integer对象,指向不同的两个Integer对象,都为false
方案四:
我先对a、f这两个对象,使用int和new Integer这两个不同的初始化方法,在对这两个对象进行比较;
int a = 128;
Integer f = new Integer("128");
if (a == f){
System.out.println("true");
}else{
System.out.println("false");
}
返回结果:true
得出结论:int和integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比
以上就是我通过实际运用所得出的结论,如果大家觉得有什么不对的地方,欢迎指示。