对于非对象类型,java 参数传递都是值传递, 比如int.
java 会直接复制一份值到方法参数里面去使用。
而对于对象类型,其实也是值传递,java 参数传递值的是对象的引用,相当于对象在堆里面的内存地址。
我们分析下以下代码:
public class StringTransactTest {
public static void main(String[] args) {
String a = "a";
String b = new String("b");
changeString(a);
changeString(b);
System.out.println(a);
System.out.println(b);
//输出是a b 因为调用changeString 的时候,我们传递的是String 对象在
// 内存中的应用,调用changeString方法的时候,会复制一份引用的值到参数栈里面,
// 在changeString方法里面,changeString 的参数变量又被重新赋值为另一个string 对象,
// 方法结束,方法外面的变量a指向的内存地址一直都没有改变过,所以,打印出来还是a.
int number = 23;
changeNum(number);
System.out.println(number);
// 打印是23,因为java 是值传递,基本数据类型会复制一份到方法栈中,在方法里面改变局部变量的值
// 不会影响外面的值。
int number1 = 10;
Integer integer11 = 10;
Integer integer12 = 10;
System.out.println(number1 == integer11);
System.out.println(integer11 == integer12);
//第一个是true number1 是基本数据类型,而integer11 是对象类型。他们两个比较的时候,
// 会发生自动拆箱,integer11 拆箱之后为基本数据类型,然后比较,肯定是相等的。
// 第二个是true 是因为Integer 直接这样写,相当于Integer.valueOf(),而这个类有缓存机制的。
// -127-128之间的数字会被缓存 当是同一个数字的时候,会直接返回同一个对象,所以,两个比较是true
int number2 = 1000;
Integer integer21 = 1000;
Integer integer22 = 1000;
System.out.println(number2 == integer21);
System.out.println(integer21 == integer22);
//第一个是true 和上面一样
// 第二个是false 是因为Integer 是因为1000已经超过了默认的缓存机制,就会去new 对象,new 的两个对象肯定会相当。
Integer integer31 = new Integer(1000);
Integer integer32 = new Integer(1000);
System.out.println(integer31 == integer32);
//两个new 的对象,在堆分配了不同的内存,所以内存地址不同 false
}
private static void changeString(String a) {
a = new String("changed");
}
private static void changeNum(int number){
number = 20;
}
}
关于Interger 的缓存机制,我们直接上源码:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
我们看到,如果值在low 和 high 之间,那么会从缓存数组里面,直接返回,否则就调用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() {}
}
这个就是Integer 的缓存。默认是-127 到 128之间,初始化的时候会直接for 循环创建这些对象,使用的时候,就可以直接用了。
参考:https://blog.csdn.net/losetowin/article/details/49968365