I\'m fairly new to Java (been writing other stuff for many years) and unless I\'m missing something (and I\'m happy to be wrong here) the following is a fatal flaw...
<This rant explains it better than I could ever even try to:
In Java, primitives are passed by value. However, Objects are not passed by reference. A correct statement would be Object references are passed by value.
It is because, it creates a local variable inside the method. what would be an easy way (which I'm pretty sure would work) would be:
String foo = new String();
thisDoesntWork(foo);
System.out.println(foo); //this prints nothing
public static void thisDoesntWork(String foo) {
this.foo = foo; //this makes the local variable go to the main variable
foo = "howdy";
}
If you think of an object as just the fields in the object then objects are passed by reference in Java because a method can modify the fields of a parameter and a caller can observe the modification. However, if you also think of an object as it's identity then objects are passed by value because a method can't change the identity of a parameter in a way that the caller can observe. So I would say Java is pass-by-value.
@Axelle
Mate do you really know the difference between passing by value and by reference ?
In java even references are passed by value. When you pass a reference to an object you are getting a copy of the reference pointer in the second variable. Tahts why the second variable can be changed without affecting the first.
This is because inside "thisDoesntWork", you are effectively destroying the local value of foo. If you want to pass by reference in this way, can always encapsulate the String inside another object, say in an array.
class Test {
public static void main(String[] args) {
String [] fooArray = new String[1];
fooArray[0] = new String("foo");
System.out.println("main: " + fooArray[0]);
thisWorks(fooArray);
System.out.println("main: " + fooArray[0]);
}
public static void thisWorks(String [] foo){
System.out.println("thisWorks: " + foo[0]);
foo[0] = "howdy";
System.out.println("thisWorks: " + foo[0]);
}
}
Results in the following output:
main: foo
thisWorks: foo
thisWorks: howdy
main: howdy
Your question as asked doesn't really have to do with passing by value, passing by reference, or the fact that strings are immutable (as others have stated).
Inside the method, you actually create a local variable (I'll call that one "localFoo") that points to the same reference as your original variable ("originalFoo").
When you assign "howdy" to localFoo, you don't change where originalFoo is pointing.
If you did something like:
String a = "";
String b = a;
String b = "howdy"?
Would you expect:
System.out.print(a)
to print out "howdy" ? It prints out "".
You can't change what originalFoo points to by changing what localFoo points to. You can modify the object that both point to (if it wasn't immutable). For example,
List foo = new ArrayList();
System.out.println(foo.size());//this prints 0
thisDoesntWork(foo);
System.out.println(foo.size());//this prints 1
public static void thisDoesntWork(List foo){
foo.add(new Object);
}