不变和传递价值 [英] Immutable and pass by value

查看:78
本文介绍了不变和传递价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,其中包含

a mutable Person类,String和修改String和Person实例的方法

I have the following code which has
a mutable Person class, String and a method to modify the instances of String and Person

    class Person{

int a = 8;

public int getA() {
    return a;
}

public void setA(int a) {
    this.a = a;
}

@Override
public String toString() {
    return "Person [a=" + a + "]";
}

  }

-

public class TestMutable {
public static void main(String[] args)
{
    Person p = new Person();
    p.setA(34);


    String s = "bar";

             modifyObject(s, p);   //Call to modify objects

    System.out.println(s);
    System.out.println(p);

}



private static void modifyObject(String str, Person p)
{

        str = "foo";
        p.setA(45);

}

  }

输出是正如所料。打印

           bar
          Person [a=45]

现在,我的问题是

在你说str =的地方发生了什么foo

最初我们假设s ='bar'且数据位于0x100内存

Initially let's assume that s='bar' and the data resides in 0x100 memory

现在将字符串的引用传递给另一个方法,另一个方法尝试使用s =foo将内存位置(0x100)的内容更改为foo。这是发生了什么,或者'foo'是在不同的内存位置创建的?

Now the reference of string is passed to another method, the other method tries to change the contents of the memory location(0x100) to 'foo' using s="foo". Is this what is happening, or is 'foo' is created in differennt memory location ?

java是否按值传递引用?

推荐答案

Java总是按值而不是通过引用传递参数。

Java always passes arguments by value NOT by reference.

让我通过示例

public class Main
{
     public static void main(String[] args)
     {
          Foo f = new Foo("f");
          changeReference(f); // It won't change the reference!
          modifyReference(f); // It will change the object that the reference variable "f" refers to!
     }
     public static void changeReference(Foo a)
     {
          Foo b = new Foo("b");
          a = b;
     }
     public static void modifyReference(Foo c)
     {
          c.setAttribute("c");
     }
}

我将分步说明:

1-声明类型 Foo 的名为 f 的引用并指定它到 Foo 类型的新对象,其属性为f

1- Declaring a reference named f of type Foo and assign it to a new object of type Foo with an attribute "f".

Foo f = new Foo("f");

2-从方法方面,类型为 Foo 的引用声明名称 a ,并且它最初被分配给 null

2- From the method side, a reference of type Foo with a name a is declared and it's initially assigned to null.

public static void changeReference(Foo a)

< img src =https://i.stack.imgur.com/k2LBD.pngalt =在此处输入图像说明>

3-当你打电话给方法 changeReference ,引用 a 将被分配给作为参数传递的对象。

3- As you call the method changeReference, the reference a will be assigned to the object which is passed as an argument.

changeReference(f);

4-声明类型为<$ c $的名为 b 的引用c> Foo 并将其分配给类型为 Foo 的新对象,其属性为b

4- Declaring a reference named b of type Foo and assign it to a new object of type Foo with an attribute "b".

Foo b = new Foo("b");

5- a = b 重新分配参考 a NOT f 指向其属性为b的对象

5- a = b is re-assigning the reference a NOT f to the object whose its attribute is "b".

6-当您调用 modifyReference(Foo c)方法时,引用 c 已创建并分配给属性为f的对象。

6- As you call modifyReference(Foo c) method, a reference c is created and assigned to the object with attribute "f".

7 - c.setAttribute(c); 将更改引用 c 的对象的属性指向它,和引用 f 指向它的对象相同。

7- c.setAttribute("c"); will change the attribute of the object that reference c points to it, and it's same object that reference f points to it.

我希望您现在了解如何将对象作为参数传递在Java中:)

I hope you understand now how passing objects as arguments works in Java :)

这篇关于不变和传递价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆