Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

References in Java. Two examples, whats the difference?

i'm having an argue with my friend.

Is:

public class Thing
{
    private Thing s;
    public void foo(Thing t)
    {
        s = t;
        t.s = this;
    }
}

The same as:

public class Thing
{
    private Thing s;
    public void foo(Thing t)
    {
        s = t;
        s.s = this;
    }
}

I think its the same since s is set to t in both cases, but he disagrees

like image 370
user978945 Avatar asked Feb 22 '23 19:02

user978945


1 Answers

They're the same since you're setting them to the same reference.

However, if you had two uses of new then the references would be different, and then your friend would be correct.

like image 104
Platinum Azure Avatar answered Mar 07 '23 10:03

Platinum Azure