【发布时间】:2014-06-04 07:52:11
【问题描述】:
我在做一些 RnD 我遇到了这个差异
我的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++;
}
}
我得到了上面的代码如下
print x10
Before removing
item one
item two
item three
item four
item five
item six
After removing
item two
item three
item four
item five
我的问题是,当我发送 Integer 来运行它时,它的行为方式与我发送 List<String> 时的行为方式相同,它的行为方式类似于参考,为什么会有这种差异?
谁能解释一下
【问题讨论】:
-
这里正在进行一场激烈的辩论:stackoverflow.com/questions/40480/…
标签: java pass-by-reference pass-by-value