考虑列表参数作为参考的Java
我在做一些研究我遇到了这个差异 我的考虑列表参数作为参考的Java,java,pass-by-reference,pass-by-value,Java,Pass By Reference,Pass By Value,我在做一些研究我遇到了这个差异 我的java代码如下 public class Main { public static void main(String[] args) { Integer x = 10; increment(x); System.out.println("print x" + x); List<String> strList = new ArrayList<String>(); strList.add
java
代码如下
public class Main {
public static void main(String[] args) {
Integer x = 10;
increment(x);
System.out.println("print x" + x);
List<String> strList = new ArrayList<String>();
strList.add("one");
strList.add("two");
strList.add("three");
strList.add("four");
strList.add("five");
strList.add("six");
System.out.println("Before removing");
for (String string : strList) {
System.out.println("item " + string);
}
removeSomeItem(strList);
System.out.println("After removing");
for (String string : strList) {
System.out.println("item " + string);
}
}
private static void removeSomeItem(List<String> strList) {
strList.remove(0);
strList.remove(4);
}
private static void increment(Integer x) {
x++;
}
}
我的问题是,当我将Integer
发送到函数时,它的行为就像值一样,当我发送List
时,它的行为就像引用为什么会有这种差异?
有人能解释一下吗?主要的区别在于Integer类是不可变的,因此为什么您看不到main方法中的更改
x++; // this will simply return a new Integer
要查看差异,请尝试以下主要方法:
x = increment(x);
在增量方法中,将其更改为:
return x++;
但是,对于列表示例,您只是将引用的副本传递给列表。只要该引用没有设置为新对象(它不是),它就能够更新您传递的原始列表。主要区别在于Integer类是不可变的,因此您没有看到main方法中的更改
x++; // this will simply return a new Integer
要查看差异,请尝试以下主要方法:
x = increment(x);
在增量方法中,将其更改为:
return x++;
但是,对于列表示例,您只是将引用的副本传递给列表。只要该引用没有设置为新对象(它不是),它就能够更新您传递的原始列表。主要区别在于Integer类是不可变的,因此您没有看到main方法中的更改
x++; // this will simply return a new Integer
要查看差异,请尝试以下主要方法:
x = increment(x);
在增量方法中,将其更改为:
return x++;
但是,对于列表示例,您只是将引用的副本传递给列表。只要该引用没有设置为新对象(它不是),它就能够更新您传递的原始列表。主要区别在于Integer类是不可变的,因此您没有看到main方法中的更改
x++; // this will simply return a new Integer
要查看差异,请尝试以下主要方法:
x = increment(x);
在增量方法中,将其更改为:
return x++;
但是,对于列表示例,您只是将引用的副本传递给列表。只要该引用没有设置为新对象(它不是),它就能够更新您传递的原始列表。这正是发生的情况
private static Integer b;
public static void main(String[] args) {
Integer x0 = 10;
b = x0;
increment(x0);
}
private static void increment(Integer x1) {
//x1 == b is true
x1++; //implies x1 = x1 + 1;
//x1 == b is now false
//at the end of the day, you've done nothing to x0 or b
}
编辑:此代码将失败,因为JVM显然正在缓存介于-128和127之间的整数值,设置x0=150
并进行测试
public class Main {
static Integer b;
public static void main(String[] args) {
Integer x = 150;
b = x;
increment(x);
}
private static void increment(Integer x) {
System.out.println(x == b); //true
x++;
System.out.println(x == b); //false
b++;
System.out.println(x == b); //false
}
}
事情就是这样
private static Integer b;
public static void main(String[] args) {
Integer x0 = 10;
b = x0;
increment(x0);
}
private static void increment(Integer x1) {
//x1 == b is true
x1++; //implies x1 = x1 + 1;
//x1 == b is now false
//at the end of the day, you've done nothing to x0 or b
}
编辑:此代码将失败,因为JVM显然正在缓存介于-128和127之间的整数值,设置x0=150
并进行测试
public class Main {
static Integer b;
public static void main(String[] args) {
Integer x = 150;
b = x;
increment(x);
}
private static void increment(Integer x) {
System.out.println(x == b); //true
x++;
System.out.println(x == b); //false
b++;
System.out.println(x == b); //false
}
}
事情就是这样
private static Integer b;
public static void main(String[] args) {
Integer x0 = 10;
b = x0;
increment(x0);
}
private static void increment(Integer x1) {
//x1 == b is true
x1++; //implies x1 = x1 + 1;
//x1 == b is now false
//at the end of the day, you've done nothing to x0 or b
}
编辑:此代码将失败,因为JVM显然正在缓存介于-128和127之间的整数值,设置x0=150
并进行测试
public class Main {
static Integer b;
public static void main(String[] args) {
Integer x = 150;
b = x;
increment(x);
}
private static void increment(Integer x) {
System.out.println(x == b); //true
x++;
System.out.println(x == b); //false
b++;
System.out.println(x == b); //false
}
}
事情就是这样
private static Integer b;
public static void main(String[] args) {
Integer x0 = 10;
b = x0;
increment(x0);
}
private static void increment(Integer x1) {
//x1 == b is true
x1++; //implies x1 = x1 + 1;
//x1 == b is now false
//at the end of the day, you've done nothing to x0 or b
}
编辑:此代码将失败,因为JVM显然正在缓存介于-128和127之间的整数值,设置x0=150
并进行测试
public class Main {
static Integer b;
public static void main(String[] args) {
Integer x = 150;
b = x;
increment(x);
}
private static void increment(Integer x) {
System.out.println(x == b); //true
x++;
System.out.println(x == b); //false
b++;
System.out.println(x == b); //false
}
}
那么万一
removeSomeItem(strList);
您正在将原始ArrayList的地址传递给该方法,因此当它使用该引用删除某些值时,原始ArrayList也会更改(实际上它们是具有两个访问点的单个对象,我指的是引用)
但是如果是整数,您也会传递引用,而increment(Integer x)方法会接收原始引用。但当我做类似的事情时,整数是不可变的
x++;
在后台,它的工作就像
x=new Integer(x+1);
这就是为什么当ArrayList更改时,原始整数保持不变。如果
removeSomeItem(strList);
您正在将原始ArrayList的地址传递给该方法,因此当它使用该引用删除某些值时,原始ArrayList也会更改(实际上它们是具有两个访问点的单个对象,我指的是引用)
但是如果是整数,您也会传递引用,而increment(Integer x)方法会接收原始引用。但当我做类似的事情时,整数是不可变的
x++;
在后台,它的工作就像
x=new Integer(x+1);
这就是为什么当ArrayList更改时,原始整数保持不变。如果
removeSomeItem(strList);
您正在将原始ArrayList的地址传递给该方法,因此当它使用该引用删除某些值时,原始ArrayList也会更改(实际上它们是具有两个访问点的单个对象,我指的是引用)
但是如果是整数,您也会传递引用,而increment(Integer x)方法会接收原始引用。但当我做类似的事情时,整数是不可变的
x++;
在后台,它的工作就像
x=new Integer(x+1);
这就是为什么当ArrayList更改时,原始整数保持不变。如果
removeSomeItem(strList);
您正在将原始ArrayList的地址传递给该方法,因此当它使用该引用删除某些值时,原始ArrayList也会更改(实际上它们是具有两个访问点的单个对象,我指的是引用)
但是如果是整数,您也会传递引用,而increment(Integer x)方法会接收原始引用。但当我做类似的事情时,整数是不可变的
x++;
在后台,它的工作就像
x=new Integer(x+1);
这就是为什么当ArrayList更改时原始整数保持不变。这里正在进行一场激烈的辩论:这里正在进行一场激烈的辩论:这里正在进行一场激烈的辩论:这里正在进行一场激烈的辩论:它将x的值增加1,对吗?x++相当于x=x+1是,但与您传递的原始对象引用不同,它创建了一个新对象,该对象在方法结束时会立即被丢弃。谢谢@JamesB,所以您的意思是,无论何时发送列表,它都不会被引用value@SharanabasuAngadi你会得到一份参考资料。我知道术语很混乱,但Java中的一切都是按值传递的。它将x的值增加1,对吗?x++相当于x=x+1是,但不是您传递的原始对象引用,而是它创建的