Java 是按值传递吗?

发布于 2024-12-05 12:26:04 字数 899 浏览 3 评论 0原文

可能的重复:
Java 是按引用传递吗?

为什么我的这段代码不起作用?我将一个对象传递给一个方法,但它不会修改我在 main 中创建的原始对象,这是为什么?

public class TestRun{

        public static void main(String[] args){
            Test t1 = new Test();
            Test t2 = new Test();
            t2.x = 555;
            t2.y = 333;

            System.out.println("The original valuess of X and Y are");
            System.out.println("X =  "+t1.x+"Y = "+t1.y);
            modifyObject(t1,t2);
            System.out.println("After the modification ");
            System.out.println("X =  "+t1.x+"Y = "+t1.y);


        }

        public static void modifyObject(Test arg1,Test arg2){
            arg1 = arg2;
        }
}

public class Test{

        int x = 9999;
        int y = 1;
}

Possible Duplicate:
Is Java pass by reference?

How come this code of mine is not working? I am passing an object into a method but it won't modify the original object that I created in main, why is that?

public class TestRun{

        public static void main(String[] args){
            Test t1 = new Test();
            Test t2 = new Test();
            t2.x = 555;
            t2.y = 333;

            System.out.println("The original valuess of X and Y are");
            System.out.println("X =  "+t1.x+"Y = "+t1.y);
            modifyObject(t1,t2);
            System.out.println("After the modification ");
            System.out.println("X =  "+t1.x+"Y = "+t1.y);


        }

        public static void modifyObject(Test arg1,Test arg2){
            arg1 = arg2;
        }
}

public class Test{

        int x = 9999;
        int y = 1;
}

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

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

发布评论

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

评论(4

罪#恶を代价 2024-12-12 12:26:04

你已经在标题中回答了。 Java是“按值传递”。

这意味着该方法仅接收对象引用 arg1arg2副本

但是,您可以通过执行以下操作来更改对象的内容

arg1.x=arg2.x
ary1.y=arg2.y

You answered it in the title. Java is "pass by value".

This means that the method receives only copies of the object references arg1 and arg2.

You can, however, alter the contents of the objects, by doing

arg1.x=arg2.x
ary1.y=arg2.y
绝不服输 2024-12-12 12:26:04

它的值传递是的。

但您需要认识到 Test 是引用类型。在 C 语言中,它们是指针。

因此,在调用modifyObject(arg1,arg2)时,您实际上所做的是复制指针的值(即复制指针的内存地址,或复制引用变量的值)。

Its pass by value yes.

But what you need to recognise is that Test are reference types. In C, they would be pointers.

So what you're in fact doing in when calling modifyObject(arg1,arg2) is copying the values of the pointers (i.e. copying the memory address of the pointer, or copying the value of the variable with is the reference).

送舟行 2024-12-12 12:26:04

在函数modifyObject中:

arg1接收Test类型的对象的地址,假设x1000
arg2 接收对象的另一个地址,假设 x2000

arg1 包含对对象的引用,但如果执行 arg1 = arg2,则只是告诉 arg1 指向另一个地址。

这样,您就不会触及 arg1 指向的对象的内容

。 arg1 和 arg2 是函数中的局部变量。要对对象进行更改,您需要寻址到存储它的内存,但 arg1.x 或 arg1.y

注意到该对象位于内存中的某个位置,而 arg1/arg2/t1/t2 只是说明它们的位置位于,它们不是“实际”对象

in function modifyObject:

arg1 receives an address of an object of type Test, let's say x1000
arg2 receives another address of an object, let's say x2000

arg1 contains a reference to an object, but if you do arg1 = arg2, you are just telling arg1 to point to another address.

this way, you are not touching the content of the object pointed by arg1

arg1 and arg2 are local variables in the function. to make changes to the object, you need to address to the memory where it is stored, but arg1.x or arg1.y

notice that the object is somewhere in the memory, and arg1/arg2/t1/t2 are just saying where they are located, they are not the "actual" objects

可遇━不可求 2024-12-12 12:26:04

呃?!!

你的期望是什么?因此,您创建并打印对象 t1,其 x,y 值分别为 999,1。如果你打印它,这就是你会得到的

$ java TestRun
The original valuess of X and Y are
X =  9999, Y = 1
After the modification 
X =  9999, Y = 1

那么...你的问题是什么?

  • Java 是按值传递吗? A:是
  • Q不会修改原来的对象吗? A:没错,不会修改。
  • 这是为什么呢? A:因为Java是按值传递的

Uh?!!

What was your expectations? So, you create and print object t1 whose x,y values are 999,1 respectively. If you print it, that's what you'll get

$ java TestRun
The original valuess of X and Y are
X =  9999, Y = 1
After the modification 
X =  9999, Y = 1

So... what is your question again?

  • Q Is Java pass by value? A: Yes
  • Q It won't modify the original object? A: That's correct, it won't be modified.
  • Q Why is that? A: Because Java is pass by value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文