-1

I have the following code:

public class PassReferenceByValue {


    static void modify(String m)
    {
       m = "Else";
    }


    public static void main(String [] arg)
    {
       String actual = "Something";

       modify(actual);

       System.out.println(actual);

    }
}

It will print Something.

I get that Java doesn't pass objects at all. Instead, it creates copy of the reference passed. If i understood correctly, when I call modify(actual) Java creates another reference to the same object. So, now we have two references that reference to the object actual. Now, through the second reference, we modify the object and the object should change. The object actual should change, because through the copied reference we have the same access to the object.

Can somebody explain me where I fail to understand the concept of passing references by value?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Want
  • 820
  • 2
  • 9
  • 20
  • http://stackoverflow.com/questions/40480/is-java-pass-by-reference – rajesh May 06 '13 at 11:20
  • 1
    (And look at the various other related questions on the right.) – Jon Skeet May 06 '13 at 11:21
  • m = "Else"; don't modify any object. It create a new string object which contains the string "Else" and then change m to reference this new object. – MTilsted May 06 '13 at 11:23
  • 1
    Disagree with the duplicate inasmuch as answers to the referenced question do not clear up the questioner's point of confusion. Might be better to edit the question so it's asking "Does String reassignment change the underlying object, or create a new String and assign the reference to it?" – Andrew Spencer May 06 '13 at 11:25
  • @Want check out [this link](https://dl.dropboxusercontent.com/u/1431684/javatemp.png) out. It should clear your confusion. – jlordo May 06 '13 at 11:31

5 Answers5

0
static void modify(String m)
    {
       m = "Else";
       System.out.println(m);
    }

If you do this m will print Else, it is only the local variable m what is getting assigned to a new value. value of actual in the main method is not getting changed here.

Vineet Singla
  • 1,609
  • 2
  • 20
  • 34
0

All "variables" in Java are references to address in a memory when the object resides.

So you have:

actual -> String("Something")

after method invocation you have

actual -> String("Something")

and

m -> String("Something")

You are just changing m -> String("Else")

bot not actual -> String("Something")

Where String("...") notation means object String with value "...".

Michal Borek
  • 4,584
  • 2
  • 30
  • 40
  • Also, as soon as you leave `modify()` the variable `m` has gone out of scope and so neither it nor the String it references can be used. – Andrew Spencer May 06 '13 at 11:22
0

Check this

Java's pass by value and pass by reference is nicely explained

G.S
  • 10,413
  • 7
  • 36
  • 52
0
  1. Strings are immutable, but that's a different discussion
  2. You are not modifying the "object" pointed by String reference actual. In the method modify() you are just overwriting the reference to point to a new String object called "Else". Now this doesn't make any "effect" to the object pointed by actual. Hence when you print actual, it still remains the same

Why I am trying to make point 1 is, say your modify method was like below

public void modify(String s) {
   s.replaceAll("Some", "Many");
}

Still the actual would have printed "Something" because of String's immutable behavior.

sanbhat
  • 17,522
  • 6
  • 48
  • 64
0

You're changing m which is a local variable only visible within modify() referencing an object. You're creating a new String object with the value "Else" and making m point towards it.

When you then come out of modify() and print actual, it is still a reference to the String object containing the text "Something".

Andrew Spencer
  • 15,164
  • 4
  • 29
  • 48