4

Possible Duplicate:
Is Java pass by reference?

So consider the following two examples and their respective output:

public class LooksLikePassByValue {

    public static void main(String[] args) {

        Integer num = 1;
        change(num);
        System.out.println(num);
    }

    public static void change(Integer num)
    {
        num = 2;
    }
}

Output:

1


  public class LooksLikePassByReference {

    public static void main(String[] args) {

        Properties properties = new Properties();
        properties.setProperty("url", "www.google.com");
        change(properties);
        System.out.println(properties.getProperty("url"));
    }

    public static void change(Properties properties2)
    {
        properties2.setProperty("url", "www.yahoo.com");
    }
}

Output:

www.yahoo.com

Why would this be www.yahoo.com? This doesn't look like passbyvalue to me.

Community
  • 1
  • 1
Shawn
  • 7,235
  • 6
  • 33
  • 45
  • See also: http://stackoverflow.com/questions/40480/is-java-pass-by-reference –  Aug 08 '11 at 18:57

5 Answers5

10

The reference is passed by value. But the new reference is still pointing to the same original object. So you modify it. In your first example with the Integer you are changing the object to which the reference points. So the original one is not modified.

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
2

Your first example does:

num = 2

That is the same as

num = new Integer(2)

So you see how it is not quite the same as your second example. If Integer let you set the value in it, you could have done:

num.setValue(2) // I know Integer doesn't allow this, but imagine it did.

which would have done exactly what the second example did.

shoebox639
  • 2,312
  • 15
  • 14
1

This is pass-by-value, but the value is the reference to properties, and you don't change it, only some inner field of it.

In the first case you change the reference, not any member of the reference, while in the second you change a member of the reference, but leave the reference as is.

MByD
  • 135,866
  • 28
  • 264
  • 277
1

Try this:

 public class LooksLikePassByReference {

    public static void main(String[] args) {

        Properties properties = new Properties();
        properties.setProperty("url", "www.google.com");
        change(properties);
        System.out.println(properties.getProperty("url"));
    }

    public static void change(Properties properties2)
    {
        properties2 = new Properties();
        properties2.setProperty("url", "www.yahoo.com");
    }
}

It prints out "www.google.com".

You are actually passing the value of the reference, so changes made to the object via that reference will be seen. If you assign a new object reference to the parameter, though, that change will not be reflected, since you've only passed the value of the reference, rather than an actual reference to a variable.

dlev
  • 48,024
  • 5
  • 125
  • 132
0

That's because properties2 is nothing more than an object reference. This means the references passed to the method are actually copies of the original references. As this illustrates,

enter image description here