Why can't my Java method change a passed varia

2019-04-07 08:09发布

This question already has an answer here:

I was kind of baffled when I saw the following code did not work as expected.

I thought Java always passed variables by references into functions. Therefore, why can't the function reassign the variable?

public static void main(String[] args) {

  String nullTest = null;

  setNotNull(nullTest);

  System.out.println(nullTest);
}

private static void setNotNull(String s) {
  s = "not null!";
}

This program outputs null.

4条回答
贼婆χ
2楼-- · 2019-04-07 08:51

References to objects are passed by value in Java so assigning to the local variable inside the method doesn't change the original variable. Only the local variable s points to a new string. It might be easier to understand with a little ASCII art.

Initially you have this:

------------
| nullTest |
------------
     |
    null

When you first enter the method setNotNull you get a copy of the value of nullTest in s. In this case the value of nullTest is a null reference:

------------    ------------
| nullTest |    |    s     |
------------    ------------
     |               |
    null            null

Then reassign s:

------------    ------------
| nullTest |    |    s     |
------------    ------------
     |               |
    null         "not null!"

And then leave the method:

------------
| nullTest |
------------
     |
    null
查看更多
Summer. ? 凉城
3楼-- · 2019-04-07 09:05

I was hoping to do something like setNotNull(MyObject o) without using o = setNotNull(o)

Simply, you cannot. The closest you will get is something like this:

public class MyRef<T> {
    private T obj;

    public T get() {
        return obj;
    }

    public void set(T obj) {
        this.obj = obj;
    }

    public void setNotNull(T obj) {
        if (this.obj == null) {
            this.obj = obj;
        }
    }
}

MyRef<MyObj> ref = new MyRef<MyObj>();
ref.setNotNull(xyz);
System.err.println(ref.get());

which is all rather clunky, and probably not worth the effort.

查看更多
一纸荒年 Trace。
4楼-- · 2019-04-07 09:07

Java doesnt pass by reference, it passes the value of the reference. When you are assigning s="not null", you are reassigning that value.

查看更多
ら.Afraid
5楼-- · 2019-04-07 09:09
s =

this is why. you are assigning to s, not modifying the object that s points to.

查看更多
登录 后发表回答