4

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.

Deepak Kumar
  • 843
  • 1
  • 7
  • 19
  • 4
    I did read. Java is pass by value. All `Object` types have a value that **is** a reference. – Elliott Frisch Nov 26 '15 at 07:18
  • 2
    "At the same time there are few people who stick to the fact that it is pass by reference in case when we pass objects to methods as parameter." They are wrong, plain and simple. C# is pass-by-value by default, but has the `ref` modifier for pass-by-reference parameters. See http://pobox.com/~skeet/csharp/parameters.html – Jon Skeet Nov 26 '15 at 07:21
  • 2
    Some people may prefer to look at it that way to help them "come-to-grips" with the simple concept. Nonetheless, they are incorrect. I would also argue "...when we pass objects to methods..." is the exact case that helps prove it to be pass-by-value. The behavior of a reference passed as an argument in Java isn't what you expect with pass-by-reference. – ChiefTwoPencils Nov 26 '15 at 07:39
  • @Jon Skeet There are also people who think that this was not even topic, taking into account, that Java does not deal de-referentiation, at all. They also wonder about grammar and semantics of "Java is " and they wonder, why exactly Java-ists need to explain to others what "Call by Reference" should mean, like a vegan speaking in front of a butchers' guild on pig farming. – Sam Ginrich Jun 03 '23 at 13:42

4 Answers4

4

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.

Michael Lloyd Lee mlk
  • 14,561
  • 3
  • 44
  • 81
  • Thanks for the answer.. I actually saw the article on Java Ranch.. I had included that in my question and also I had included all the details but somebody marked it duplicate and also I was getting down votes. So I actually edited my question and then all was fine. People are simple careless and don't even care to check what part of the question is different from the others.. I just wanted to ask about the details which I have highlighted in bold but at the same time I wanted to make sure that I include all the relevant details so that it does not seem that I don't know the background info.. – Deepak Kumar Nov 26 '15 at 11:47
  • 1
    @DeepakKumar: Please don't blame other people for your question not being clear. The first 75% of it *was* a duplicate of previous questions, and the title was unhelpful too. The reason the question has remained open after editing is that now it is a reasonable question, whereas before it was unclear. – Jon Skeet Nov 26 '15 at 13:19
3

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.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

C++ has different ways to pass parameters. One way is to specify a pointer type as parameter. I think that's close to what Java does. The pointer is passed by value. You can modify the state the object the pointer references but you cannot change the object instance the calling function is referencing.

I included a C++ example with two functions. One function with call by value and one with call by reference semantic. The function with call by reference semantic can change the object instance the calling function is referencing.

#include <iostream>

using namespace std;

class ClassA
{

public:
    int a = 0;

};

void funcByValue(ClassA *param)
{
    param->a = 1;
    param = new ClassA();
}

void funcByReference(ClassA* &param)
{
    param->a = 1;
    param = new ClassA();
}


int main()
{
    ClassA *objectA = new ClassA();

    cout << objectA->a << "\n";     //1
    cout << objectA << "\n";        //0000002B1D355AF0

    //This call changes objectA's state
    funcByValue(objectA);
    cout << objectA->a << "\n";     //1
    cout << objectA << "\n";        //0000002B1D355AF0

    //This call changes the object objectA is referencing
    funcByReference(objectA);
    cout << objectA->a << "\n";     //0
    cout << objectA << "\n";        //0000002B1D345610
}
Emanuel Seidinger
  • 1,272
  • 1
  • 12
  • 21
0

Java is Pass by Value : Best example :
http://www.javaworld.com/article/2077424/learn-java/does-java-pass-by-reference-or-pass-by-value.html

C++ is pass by reference and pass by value as well:
http://www.tech-recipes.com/rx/1232/c-pointers-pass-by-value-pass-by-reference/

Ashish Ani
  • 324
  • 1
  • 7