Integer是值传递还是引用传递的问题
先上代码:
ublic class Demo2_5 {
public static void main(String[] args) {
Integer a=new Integer(1);
Integer b=new Integer(2);
swap(a,b);
System.out.println("a="+a+" b="+b);
}
public static void swap(Integer a,Integer b){
Integer temp=a;
a=b;
b=temp;
}
}
运行结果:a=1 b=2
Integer是基本数据类型int的包装类,目的是可以像操作对象一样去操作基本数据类型
值传递与引用传递:
值传递:(基本数据类型的数据的传递)
基本数据类型的数据做为方法的参数进行传递
那么在方法中会另外开辟一个新的内存空间
跟原来的内存空间不是一个内存空间
如果在方法中对参数的值进更改
不会影响原来内存空间的值
如果非要获取方法中修改后的值
则需要把改动后的值返回出来.
引用传递:(数组的传递,和对象的传递)
如果传递的是对象或数组
实际上是把对象或数组的引用传递到方法中
如果在方法中对引用的对象或数组做任何修改
都会影响原来的内存空间的值,即就是修改了原来
的内存空间,其实操作是同一份内存空间
按照这个说法,Integer属于类,上面的a和b是Integer的实例化对象,应该为引用传递,是可以修改值的,但是实际情况是并没有
在看一下Integer的源码:
/**
* The value of the {@code Integer}.
*
* @serial
*/
private final int value;
/**
* Constructs a newly allocated {@code Integer} object that
* represents the specified {@code int} value.
*
* @param value the value to be represented by the
* {@code Integer} object.
*/
public Integer(int value) {
this.value = value;
}
代码中value,也就是我们在构造Integer实例时传进去的int类型的数,是private final关键字修饰的,就是说外部的类是无法直接访问且它的值只能复制一次,当然,Integer中也就没有setter方法去改变value的值(也没必要)
在刚开始提出的问题中,调用swap方法时传递给Integer类型变量a,实际上是main方法中a的引用的复制,在进行a=b等操作时,修改的只是复制引用的指向,对原来的a并没有产生任何影响,因为Integer源码中并没有提供修改其value值的方法
如图
交换前
交换后
由于复制的a的引用为局部变量,所以swap方法结束后,它就没法再使用了
总结:Integer中的value是私有的,而且也没有提供setter方法,所以对它的值是没有办法修改的,也就是没有方法使用a.value=123或者a.setter(123)去改变它的值,而且swap方法中的引用a只是main方法中主方法的复制,所以对于a=b之类的操作都只是改变了a的复制引用的值,而不是原本a,而Integer由于它的value是私有,而且没有setter方法,所以就没法改变Integer的值
Integer初始化的问题
代码:
public static void main(String[] args) {
Integer a=123;
Integer b=123;
System.out.println(a==b);
Integer c=128;
Integer d=128;
System.out.println(c==d);
}
}
运行结果:
true
false
Integer源码:
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
/**
* 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() {}
}
可以看出,Integer类中的valueOf()是从一个名为IntegerCacahe的类中cacahe中得到的,IntegerCacahe中有一个static代码块,他会将-128~127的整数名为cache的Integer数组中,所以,当一个数的大小在-128~127之间时,会直接从常量池中获得一个它的引用,但是当超过这个范围之后,便会新开辟一块空间来存储这个数