Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass by "Reference Value"? Some clarification needed

Tags:

java

I know that in Java, everything is passed by value. But for objects, it is the value of the reference to the object that is passed. This means that sometimes an object can get changed through a parameter, which is why, I guess, people say, Never modify parameters.

But in the following code, something different happens. s in changeIt() doesn't change when you get back to main():

public class TestClass {

    static String str = "Hello World";

    public static void changeIt( String s ) {
        s = "Good bye world";
    }

    public static void main( String[] args ) {
        changeIt( str );
        System.out.println( str );
    }
}

I'm guessing -- and I'd like confirmation -- that when you say s = "something" it's the same or equivalent to saying String s = new String("something"). Is this why s doesn't change? Is it assigned a whole new object locally which gets thrown away once you exit changeIt()?

like image 494
ksnortum Avatar asked Jul 20 '12 19:07

ksnortum


People also ask

What is pass by value and pass-by-reference?

Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that's why it's called pass by value. Pass by Reference: An alias or reference to the actual parameter is passed to the method, that's why it's called pass by reference.

Why would you want to pass-by-reference?

Use pass-by-reference if you want to modify the argument value in the calling function. Otherwise, use pass-by-value to pass arguments. The difference between pass-by-reference and pass-by-pointer is that pointers can be NULL or reassigned whereas references cannot.

What would be the concern when passing by reference is applied?

Pass by reference (also called pass by address) means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function so that a copy of the address of the actual parameter is made in memory, i.e. the caller and the callee use the same variable for the parameter.

What does pass by value mean?

By definition, pass by value means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter. Use pass by value when when you are only "using" the parameter for some computation, not changing it for the client program.


2 Answers

that when you say s = "something" it's the same or equivalent to saying String s = new String("something")

Yes, pretty much. (though the JVM might do optimizations so that the same string literal used several times refers to the same String object).

Is this why s doesn't change? Is it assigned a whole new object locally which gets thrown away once you exit changeIt()

Yes. As you say, everything is passed by value in Java, even references to object. So the variable s in changeIt( String s ) is a different value from str you use in main(), it's just a local variable within the changeIt method. Setting that reference to reference another object does not affect the caller of changeIt.

Note that the String object s refer to is still the same String as str refers to when entering the changeIt() method before you assign a different object to s

There's another thing you need to be aware of, and that is that Strings are immutable. That means that no method you invoke on a string object will change that string. e.g. calling s.toLowerCase() within your changeIt() method will not affect the caller either. That's because the String.toLowerCase() does not alter the object, but rather returns a new String object.

like image 164
nos Avatar answered Sep 30 '22 04:09

nos


When you write

s = "Good bye world";

you are changing the value of s to be a reference to the new string. You are not changing the value of the string referenced by s.

like image 22
Geoff Reedy Avatar answered Sep 30 '22 05:09

Geoff Reedy