1
static void f(String s)
{
    s = "x";
}

public static void main(String[] args) {
  String s = null;
  f(s);
}

Why the value of s after calling f(s) is null instead of "x"?

Ufx
  • 2,595
  • 12
  • 44
  • 83
  • It's worth noting that although most veteran Java programmers intuitively understand why it works this way, it's difficult to explain. The whole "pass reference by value" is a tough concept to grok, much less explain. Good question. – Greg Case Dec 16 '11 at 01:32
  • Noooo not again. Please [read this](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) – James Bassett Dec 16 '11 at 03:28

5 Answers5

5

Because s is a reference. You pass a copy of that reference to the method, and then modify that copy inside the method. The original doesn't change.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • As @Oli says. You might consider using final in your method parameters, that is, declare: static void f(final String s) to avoid this potential source of confusion – Miquel Dec 16 '11 at 01:18
  • When I pass a pointer in C++ I pass a copy of the pointer and I get expected result. What's the difference? It is copy of reference, not copy of object. – Ufx Dec 16 '11 at 01:20
  • 1
    @user: this is identical to `void f(char *s) { s = "x"; } int main() { char *s = NULL; f(s); }`. – Oliver Charlesworth Dec 16 '11 at 01:21
2

When passing an Object variable to a function in java, it is passed by reference. If you assign a new value to the object in the function, then you overwrite the passed in reference without modifying the value seen by any calling code which still holds the original reference.

However, if you do the following then the value will be updated:

public class StringRef
{
  public String someString;
}

static void f(StringRef s)
{
  s.someString = "x";
}

public static void main(String[] args)
{
  StringRef ref = new StringRef;
  ref.someString = s;
  f(ref);
  // someString will be "x" here.
}
Trevor Freeman
  • 7,112
  • 2
  • 21
  • 40
0

Within the function f() the value will be "x". Outside of this function the value of s will be null. Reference data types (such as objects) are passed by value see here (read the section "Passing Reference Data Type Arguments")

cyber-monk
  • 5,470
  • 6
  • 33
  • 42
0

Given that s is of type String, which is a reference type (not a primitive):

s = "x";

does not mean "transform the thing that s refers to into the value "x"". (In a language where null is a possibility, it can't really do that anyway, because there is no actual "thing that s refers to" to transform.)

It actually means "cause s to stop referring to the thing it currently refers to, and start referring to the value "x"".

The name s in f() is local to f(), so once the function returns, that name is irrelevant; the name s in main() is a different name, and is still a null reference.

The only thing that the parameter passing accomplishes is to cause s in f() to start out as null.

This explanation would actually go somewhat more smoothly if you hadn't used null :(

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
0

Actually you are not changing the value you are creating new one, it is totally different, If you change an attribute of the abject in the method then i will be changed in your references. But you are creating new object.

Elbek
  • 3,434
  • 6
  • 37
  • 49