1

I have written following program to test the equals and Java pass by value functionality (as per documentation Java is pass by value), so when I pass object a to the method I am actually passing the heap values. What is if I change the reference of the object in the method? What will happen?

package equalstest;

public class EqualsMethodTest {

    public static void main(String[] args) {
        EqualsMethodTest a = new EqualsMethodTest();
        EqualsMethodTest b = new EqualsMethodTest();
        System.out.println(" Comparing a and B");
        System.out.println(a == b);
        System.out.println(a.equals(b));
        EqualsMethodTest c = compare(a, b);

        System.out.println(" Comparing B and C after interchanging references");

        System.out.println(b == c);
        System.out.println(b.equals(c));

        System.out.println(" Comparing A and C after interchanging references");

        System.out.println(a == c);
        System.out.println(a.equals(c));
    }

    static EqualsMethodTest compare(EqualsMethodTest a, EqualsMethodTest b) {
        EqualsMethodTest c = a;
        a = b;
        return c;
    }
}

I am getting below output:

Comparing a and B
before alteration a == b false
before alteration a.equals b false
Comparing B and C after interchanging references
after alteration b == c false
after alteration b.equals c false
Comparing A and C after interchanging references
after alteration a == c true
after alteration a.equals c true

  • What actually happens in the method compare?
  • When I create a new reference c does it point to memory location of a?
  • What happens when I assigning b to a?
Tom
  • 16,842
  • 17
  • 45
  • 54
Vinay b
  • 139
  • 1
  • 2
  • 15
  • 1
    Yes, `c` actually has the same reference as `a` after the method compare is called. Also nothing happend when you assign `b` to `a`, since you did in a local scope `compare`, which wont affect the original references in your main method. in addition [the common SO question for this topic answers this well multiple times](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1) – SomeJavaGuy Sep 28 '15 at 07:11
  • 1
    You probably overlooked the fact that objects are not passed or assigned in Java but a reference (pointer) to them. – Henry Sep 28 '15 at 07:14
  • `compare` does _nothing_ except return its first argument. – Louis Wasserman Sep 28 '15 at 16:45

4 Answers4

2

When i create a new reference c does it point to memory location of a?

Yes because you didn't actually create c(with new), you just told to point c to a.

What happends when i assigning b to a ?

Same thing here too, your a points to b from now on. Hence finally your c also point's a where you pointed b to a.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

A reference can point to new object or any existing object.

EqualsMethodTest a = new EqualsMethodTest();
   // Reference--^    ^--- new object

So when you do EqualsMethodTest c = a;, it means c will point to the object whom a is pointing. So basically at this point of time c and a both references are pointing to same object. Similarly it happens for a = b;.


Pass by value confusion

Java references are passed by value.

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
1

Calling Java "pass-by-value" is, although technically correct, quite confusing. Java is pass-by-value, but the values which are passed are references. So what happens in your method is the following:

  1. EqualsMethodTest c = a; The value of the the new variable c is made equal to the value of parameter a. Thus, c now is a reference to the same object as a.
  2. a = b; The value of the variable a is made equal to the value of b. Thus a and b are now references to the same object.
  3. return c; The value of c, which is a reference to the object that a originally pointed to, is returned.

Because Java is pass-by-value, nothing changes in the code which calls compare. So if you call

`compare(a,b);`

the variabes a and b still point to the same objects as before; the change made by the second line of the compare method is local to the method.

Hoopje
  • 12,677
  • 8
  • 34
  • 50
  • When i do a=b now a points to b and since c pointed to a should not c point to where a is now pointing which is nothing but b. – Vinay b Sep 28 '15 at 07:35
  • @Vinay b. No. The `EqualsMethodTest c = a` comes *before* the `a = b`. So `c` points to the object `a` originally pointed to. Only afterwards the value of `a` is changed. You have to distinguish between the reference and the object the reference refers to. – Hoopje Sep 28 '15 at 07:51
  • when a=b here reference of a is pointing to location of b is it ? – Vinay b Sep 28 '15 at 08:01
  • It points to the same object that `b` points to, yes. But `a = b` only changes `a`, not `c`, so `c` still points to the object that `a` originally pointed to. – Hoopje Sep 28 '15 at 08:10
0

Every time you declare EqualsMethodTest you are actually creating a pointer to one. So in your compare() function you are creating a new pointer, c, which points to the same place as a. Then you are telling the pointer a to point to location b.

Before the change you had pointer a pointing to value a and pointer b pointing to value b. After the change you have the pointer c pointing to value a, the pointer a pointing to value b, and the pointer b pointing to value b.

Spartak Singh
  • 268
  • 1
  • 8