Java静态方法参数

发布于 2024-08-30 19:33:28 字数 719 浏览 10 评论 0原文

为什么以下代码返回 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() + " ");
}
}

为什么它似乎将 Hotel 按值传递给 doStuff() ?

Why does the following code return 100 100 1 1 1 and not 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() ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

懒猫 2024-09-06 19:33:28

它完全按照您告诉它的方式执行:-)

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

正如其他人指出的那样(并且在本文中解释得很清楚 ),Java 按值传递。在本例中,它将引用 h1 的副本传递给 doStuff。在那里,副本被新引用覆盖(然后返回并分配给 h2),但 h1 的原始值不受影响:它仍然引用第一个 Hotel房间号为 100 的对象。

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 h1 to doStuff. There the copy gets overwritten with a new reference (which is then returned and assigned to h2), but the original value of h1 is not affected: it still references the first Hotel object with a room number of 100.

梦亿 2024-09-06 19:33:28

对 Hotel 的引用按值传递。

Reference to Hotel is passed by value.

从﹋此江山别 2024-09-06 19:33:28

因为 Java确实按值传递。仅在这种情况下,该值是对 Hotel 对象的引用。或者更清楚地说,Java 传递了对 h1 指向的同一对象的引用。因此,h1本身没有被修改。

Because Java does pass by value. Only in this case, the value is a reference to a Hotel object. Or to be more clear, Java passes a reference to the same Object that h1 points to. Therefore, h1 itself is not modified.

雨落星ぅ辰 2024-09-06 19:33:28

酒店引用按值传递。您仅更改了 doStuff 方法中的本地 hotel 变量并返回它,而不更改原始的 h1。如果您有 setRoomNr 方法并调用 hotel.setRoomNr(1),您可以在方法内更改原始 h1...

The Hotel reference is passed by value. You're only changing the local hotel variable in the doStuff method and returning it, not changing the original h1. You could change the original h1 from within the method if you had a setRoomNr method and called hotel.setRoomNr(1) though...

够钟 2024-09-06 19:33:28

它做得很好。在static Hotel doStuff(Hotel hotel)内,您正在创建Hotelnew实例,旧hotel引用是不变的。

It is doing fine. Inside static Hotel doStuff(Hotel hotel), you are creating a new instance of of Hotel, old hotel reference is unchanged.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文