java Java静态方法参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2731119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java static method parameters
提问by AdrianS
Why does the following code return 100 100 1 1 1and not 100 1 1 1 1?
为什么下面的代码返回100 100 1 1 1而不是100 1 1 1 1?
public class Hotel {
private int roomNr;
public Hotel(int roomNr) {
this.roomNr = roomNr;
}
public int getRoomNr() {
return this.roomNr;
}
static Hotel doStuff(Hotel hotel) {
hotel = new Hotel(1);
return hotel;
}
public static void main(String args[]) {
Hotel h1 = new Hotel(100);
System.out.print(h1.getRoomNr() + " ");
Hotel h2 = doStuff(h1);
System.out.print(h1.getRoomNr() + " ");
System.out.print(h2.getRoomNr() + " ");
h1 = doStuff(h2);
System.out.print(h1.getRoomNr() + " ");
System.out.print(h2.getRoomNr() + " ");
}
}
Why does it appear to pass Hotel by-value to doStuff() ?
为什么它似乎将 Hotel 按值传递给 doStuff() ?
回答by Péter T?r?k
It does exactly what you told it to do :-)
它完全按照您的要求执行:-)
Hotel h1 = new Hotel(100);
System.out.print(h1.getRoomNr() + " "); // 100
Hotel h2 = doStuff(h1);
System.out.print(h1.getRoomNr() + " "); // 100 - h1 is not changed, h2 is a distinct new object
System.out.print(h2.getRoomNr() + " "); // 1
h1 = doStuff(h2);
System.out.print(h1.getRoomNr() + " "); // 1 - h1 is now changed, h2 not
System.out.print(h2.getRoomNr() + " "); // 1
As others noted (and is explained very clearly in this article), Java passes by value. In this case, it passes a copy of the reference h1to doStuff. There the copy gets overwritten with a new reference (which is then returned and assigned to h2), but the original value of h1is not affected: it still references the first Hotel object with a room number of 100.
正如其他人所指出的(并且在本文中进行了非常清楚的解释),Java 通过值传递。在这种情况下,它将引用的副本传递h1给doStuff。在那里,副本被新引用覆盖(然后返回并分配给h2),但 的原始值h1不受影响:它仍然引用房间号为 100 的第一个 Hotel 对象。
回答by Roman
Reference to Hotel is passed by value.
对 Hotel 的引用是按值传递的。
回答by Justin Ardini
Because Java doespass by value. Only in this case, the value is a reference to a Hotelobject. Or to be more clear, Java passes a reference to the same Object that h1 points to. Therefore, h1 itself is not modified.
因为 Java确实按值传递。仅在这种情况下,该值是对Hotel对象的引用。或者更清楚地说,Java 传递了对 h1 指向的同一个 Object 的引用。因此,h1 本身没有被修改。
回答by froadie
The Hotel reference is passed by value. You're only changing the local hotelvariable in the doStuffmethod and returning it, not changing the original h1. You couldchange the original h1from within the method if you had a setRoomNr method and called hotel.setRoomNr(1)though...
Hotel 引用按值传递。您只是更改方法中的局部hotel变量doStuff并返回它,而不是更改原始的h1. 如果你有一个 setRoomNr 方法并调用了,你可以h1从方法中更改原始方法hotel.setRoomNr(1)......
回答by fastcodejava
It is doing fine. Inside static Hotel doStuff(Hotel hotel), you are creating a newinstance of of Hotel, old hotelreference is unchanged.
它做得很好。在 内部static Hotel doStuff(Hotel hotel),您正在创建 的new实例Hotel,旧hotel引用不变。

