Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java pass by value -- can you explain this?

Tags:

java

reference

I read this excellent article and it make sense: Java is strictly pass by value; when an object is a parameter, the object's reference is passed by value.

However, I'm totally confused as to why the following snippet could possibly work. Foo has a String member variable a which is immutable and needs to be burned in every time. The first method of burning (commented out) should work fine and it does. The second method set's a's reference to the value that was passed. It should not work if newstr is a temporary variable. The expected output results are:

Totally temp
NULL

However, I get

Totally temp
Totally temp

Why? Is it just pure luck that the temporary variable reference is still good?

public class Foo {
    String a;
    public Foo(){}
    public void burna(String newstr){
           // a = new String(newstr);
           a = newstr; /*this should not work: */ 
        }
}

public class foobar {
    Foo m_foo;
    public foobar(){};
    public void dofoo(){
        String temp = new String("Totally temp\n");
        m_foo.burna(temp);
        System.out.print(m_foo.a);
    }

}

 public static void main(String[] args) {
        Foo myfoo = new Foo();
        foobar myfoobar = new foobar();

        myfoobar.m_foo = myfoo;
        myfoobar.dofoo();
        System.out.print(myfoo.a);

    }
like image 259
Pete Avatar asked Mar 06 '26 19:03

Pete


1 Answers

Foo has a String member variable a which is immutable and needs to be burned in every time.

No it doesn't: it's not marked as final:

public class Foo {
    String a;
    ...
}

The variable is perfectly mutable - you can change it to refer to a different string at any time, which is what you do in the burna method.

I don't currently see why you think this wouldn't work:

public void burna(String newstr){
    a = newstr; /*this should not work: */ 
}

That's setting the value of a to be the value of newstr - which is a reference to a string (or null). That's all it's doing. I'm not sure what you mean by "burning in" a variable though.

You're calling burna and passing in a reference to a string with the text "Totally temp\n" - so a is set to a reference to that string.

What do you mean by "It should not work if newstr is a temporary variable." There's no such thing as a "temporary variable". There's a local variable - but an object isn't destroyed just because a variable which refers to it goes out of scope. Is that what's confusing you?

Your program has several things going on - the foobar class probably isn't helping you in terms of understanding. Could you try to simplify your code to the point where it's still confusing you, but there's less going on? Then we could isolate the source of confusion more precisely.

like image 162
Jon Skeet Avatar answered Mar 08 '26 09:03

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!