Have read a lot of stack overflow answers on this topic and also I have read many blogs. I can surely conclude that Java is pass by value.
But to convince people I need a proof of some language which is pass by ref.
So can someone give a live example of a language which is pass by reference by whose example we could relate that java is not pass by reference.
So can someone give a live example of a language which is pass by reference by whose example we could relate that java is not pass by reference
The best example of Pass-by-ref vs pass-by-value is swap.
void swap(String a, String b) {
String c = b;
b = a;
a = c;
}
void main() {
String a = "A";
String b = "B";
swap(a, b);
System.out.println(a); // A
System.out.println(b); // B
}
In this case while the variable main.a points to the same object as swap.a, you have two references to the string "A".
vs C# (IDE One) which supports by-ref.
void Swap(ref string a, ref string b) {
string c = b;
b = a;
a = c;
}
void main() {
string a = "A";
string b = "B";
Swap(ref a, ref b);
Console.WriteLine(a); // B
Console.WriteLine(b); // A
}
In this case the variable main.a and swap.a are the same reference, so changes to swap.a also happen to main.a.
So how does this differ from
void swap(StringBuilder a, StringBuilder b) {
String a1 = a.toString();
String b1 = b.toString();
a.setLength(0);
a.append(b1);
b.setLength(0);
b.append(a1);
}
void main(){
StringBuilder a = new StringBuilder("A");
StringBuilder b = new StringBuilder("B");
swap(a, b);
System.out.println(a); // B
System.out.println(b); // A
}
In this case the objects pointed to get changed. For example:
public static void main(String... agv){
StringBuilder a = new StringBuilder("A");
StringBuilder b = new StringBuilder("B");
StringBuilder alsoA = a;
swap(a, b);
System.out.println(a); // B
System.out.println(b); // A
System.out.println(alsoA); //B
}
vs in C# (IDEOne)
void Main() {
string a = "a";
string b = "b";
string alsoA = a;
Swap(ref a, ref b);
Console.WriteLine(a); // B
Console.WriteLine(b); // A
Console.WriteLine(alsoA); // A
}
Java Ranch has a good article if you are still unsure.
I don't know any languages which are pass-by-reference all the time, but C# has the ref
modifier which allows a specific parameter to use pass-by-reference. That allows you to see the difference pretty easily. For example:
using System;
public class Program
{
static void Main()
{
string x = "original x";
string y = "original y";
Modify(x, ref y);
Console.WriteLine(x);
Console.WriteLine(y);
}
static void Modify(string px, ref string py)
{
px = "modified x";
py = "modified y";
}
}
The output is
original x
modified y
That's because the assignment to px
doesn't affect x
in the calling code: the value of x
was passed to the method. But the assignment to py
does change y
, because y
was passed by reference; py
and y
are aliases for the same storage location, basically.
All of this only matters when you modify the parameters themselves. Compare the above program with this one:
using System;
using System.Text;
public class Program
{
static void Main()
{
StringBuilder x = new StringBuilder("original x");
Modify(x);
Console.WriteLine(x);
}
static void Modify(StringBuilder px)
{
px.Length = 0;
px.Append("modified x");
}
}
Now the Modify
method doesn't change the value of px
at all - instead of makes changes to the object that the value of px
refers to. No pass-by-reference is required - the value of x
is passed to the method by value, and that is copied into px
. This is like copying your home address on a piece of paper and giving it to someone. It's not copying the house itself - it's just copying the way of reaching the house. If the person you've given the paper to goes and paints the front door red, then when you go home you'll see that change.
This is why it's important to understand the difference between variables, objects and references.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With