0

I have this code. When I run it I get this result:

The Circle A : The center X: 5, the center Y 5 and the Radius 5

The Circle B : The center X: 10, the center Y 10 and the Radius 10

Change

The Circle A : The center X: 10, the center Y 10 and the Radius 5

The Circle B : The center X: 5, the center Y 5 and the Radius 10

if java does not allow to pass values as reference, how did this code allow it?

Just in case, when I look it with the debugger the direction of the object "autre" was the same as the object A.

public class Main {

    public static void main(String[] args) {

        Cercle A = new Cercle();
        Cercle B = new Cercle();
        A.creer(5, 5, 5);
        System.out.println("The Circle A : ");
        A.show();
        B.creer(10, 10, 10);
        System.out.println("The Circle B : ");
        B.show();


        System.out.println("Change");
        B.change(A);
        System.out.println("The Circle A : ");
        A.show();
        System.out.println("The Circle B : ");
        B.show();
    }
}

public class Cercle {
    public int x, y;
    public int r;

    public void creer (int n1, int n2, int n3)
    {
        x = n1;
        y = n2;
        r = n3;
    }

    public void show()
    {
        System.out.println(" The center X: "+x+", the center Y "+y+" and the Radius "+r);
    }


    public void change(Cercle autre){
        int tmp ;
        tmp = x ;
        x = autre.x;
        autre.x = tmp;
        tmp = y;
        y = autre.y;
        autre.y = tmp ;
    }

}
  • 1
    Java is actually pass by value in actual terminology, but it actually passes the reference copy of object you are passing. The whole confusion of these terminology is because of different background of developer that belongs to c , c++ and java and have different implementation. So without blindly quoting pass by value and pass by reference see this as **call-by-value[-of-the-reference].** and don't fall in trap of jagarans. And after this clarification of reference, I hope you can deduce the logic behind the output you have got – Ravi Feb 07 '19 at 08:49
  • @Ravi The situation is made far worse when people start making up their own terminology like 'reference copy'. There is no such thing. – user207421 Feb 07 '19 at 09:13
  • @user207421 With due respect to your point, I am stating the terminiology call-by-value[-of-the-reference] which is marked as bold. And this gives some clear picture and better understanding rather than existing jargons. We need to accept that existing terminiology have some flaws thats why the understanding differs. And this is marked to give just a better picture and understanding. If it helps then I don't see a problem in this. – Ravi Feb 07 '19 at 09:22

1 Answers1

0

JAVA is "pass-by-value", indeed, but here, you pass the (value of) reference (still the address of that object) to the object in the function, which is a reference itself. Any changes made to it will change the original object as well.

It's just like passing pointers as arguments to a function in C/C++ where you pass a copy of the pointer and modify it, but since the copy is an address in itself, you basically modify the address where the variable resides, thus modifying the variable in turn.

See this to understand in more detail.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
  • Thank you for your fast answer, I really apreciate it! so if want it to passed the object as value, how can I do this? – Rafael Arias Feb 07 '19 at 08:48
  • I assume by this, you don't want to modify the object being passed as an argument. In that case, you can simply create a copy of the passed object like `Obj newObj = new Obj(A);` inside the function. Any changes will then happen to the object `newObj`, leaving `A` intact. – Jarvis Feb 07 '19 at 08:50
  • What I want it is control of the pointer and understading the "Java always passes arguments by value", your responses make it more clear to me. Thank you. – Rafael Arias Feb 07 '19 at 08:56
  • 3
    Jarvis: This question is a clear duplicate. This duplicate already contains well designed and profound answer(s). You really shouldn't answer those questions but simply mark them as duplicate. Doing so encourage people to first search for such a duplicate instead of simply throwing those questions at SO. – Seelenvirtuose Feb 07 '19 at 08:56
  • Please mark the answer as accepted if it solved your issue. @RafaelArias – Jarvis Feb 07 '19 at 09:05