Java implements pass by reference right?

So when I do cons("item", stack) with the following code I should print out --> itemabcd. But that isn't the case, why?


String stack = "abcd";
cons("item",stack);
System.out.println(stack);

//
public void cons(String item, String stack)
{
stack = item + stack;
}

Java implements pass by reference right?

So when I do cons("item", stack) with the following code I should print out --> itemabcd. But that isn't the case, why?


String stack = "abcd";
cons("item",stack);
System.out.println(stack);

//
public void cons(String item, String stack)
{
stack = item + stack;
}

Strings are immutable!!! That means they cannot be changed. When you run the line stack = item + stack you are making a new string that stack (the one in cons) references, yet the stack in the main method remains unchanged.

Does this make sense?

No. Java is pass-by-value and pass-by-value only.

When you pass an object rather than a primitive, you are not actually passing an object. What you have is a variable that is only a reference to an object, and when you pass this, what happens is that the reference value is copied, and the copy is passed. Now you have two references to the same object.

Because of this you can use the methods of the object and see the result outside of the method, but changing the variable inside of the method will have absolutely no effect outside of the method.

You should probably read the following two articles. The second one is the one that is important, but you probably won't understand the terms they use if you do not read the first one.

http://www.javaranch.com/campfire/StoryCups.jsp
http://www.javaranch.com/campfire/StoryPassBy.jsp

Comments
A very old post, but a great explanation. I keep seeing people have this problem too...

Yes, this is similar to my reply. Strings cannot be changed. Therefore to change a string you have to make a new one. You will have 2 different strings in the heap.

Yes, this is similar to my reply. Strings cannot be changed. Therefore to change a string you have to make a new one. You will have 2 different strings in the heap.

No, you don't understand the difference between pass by value, pass by reference, and immutable variables.

Strings are immutable but if Java employed pass by reference that wouldn't matter.
It also doesn't matter in reality where Java employs pass by value exclusively of course.

Easy way to check:
IF Java used pass by reference the following would work to swap 2 numbers:

public void swap(int a, int b) {
    int c = a;
    a = b;
    b = c;
}

As it is that method does exactly nothing outside the method.
In C++, which has pass by reference (though it's not the default), you can do this and it works:

void swap(int& a, int& b)
{
    int& c = a;
    a = b;
    b = c;
}

As said before, the Java language is by value only, when it comes to primitive datatypes. If you need to send by reference you can use arrays, because arrays and objects that inherits from Object are always sent by reference.

Example:

public void callingMethod() {
String[] strArr = new String[1];
strArr[0] = " world!";
mergeText("Hello", strArr);
for(String strText : strArr) {
System.out.print(strText + "\t");
}
}
private void mergeText(String str1, String[] newStr) {
newStr[0] = str1 + newStr[0];
}

This will print out
Hello world!

I agree that the above method is overkill, but this is a way to send by reference instead of sending by value in Java! :D

Edited 3 Years Ago by happygeek: fixed formatting

Hi dirbacke and welcome to DaniWeb :)

Please read the forum rules at the top of this forum. Also, please take note of the date of forum threads, particularly if they have been marked as solved, before posting. However it is always nice to see a new face here at DW!

Regards,
d

Old post, but I still feel the need to respond. This is still not pass by reference. That is passing a copy of the reference value that points to the array. In which you can then change the reference values stored in the elements of the array, but it is still not pass by reference. Pass by reference would allow you to create a whole new array (i.e. new String[]) and have that change also take affect outside of the method, and that will not work of course.

Now, once more, for slow learners, there is no pass-by-reference in Java it is pass-by-value and pass-by-value only.

Edited 3 Years Ago by Nick Evan: Fixed formatting

For anyone who's still not getting it:
References are bit patterns which describe the location of an object in the JVM's memory.
References are kept inside object variables, that is: object variables contain references.
Java is pass-by-value, that is pass-by-copy, when you pass a value to a method, then its bit pattern is copied into the method's parameter variable.
So, when you pass an object variable to a method, then its value (i.e. the reference's bit pattern) is copied into the method's parameter.
But under no circumstances this should ever be called pass-by-reference, Java is pass-by-value.

Edited 6 Years Ago by mvmalderen: fix typo

This question has already been answered. Start a new discussion instead.