The ==
operator in java checks if two objects are at the same memory location. Dose this mean that if object1 != object2
then object1
is not linked with obejct2
? By linked I mean that if object1
is changed then obejct2
is also changed.

- 183
- 2
- 12
-
if object 1 and 2 are cars pointing to the same reference, then I paint car2 red, what color has car1??? – ΦXocę 웃 Пepeúpa ツ Oct 11 '16 at 18:25
-
2I think you have answered your question in the first sentence. – Jaroslaw Pawlak Oct 11 '16 at 18:25
-
If two addresses point to the same object, there is only one object. – Peter Lawrey Oct 11 '16 at 18:27
-
http://stackoverflow.com/questions/16089282/how-equals-method-works – Lyju I Edwinson Oct 11 '16 at 18:28
-
1Possible duplicate of [Compare two objects with .equals() and == operator](http://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator) – Pavneet_Singh Oct 11 '16 at 18:29
-
1If you understand this question differently it sais "Can I have two different (unequal) references to the same Object". I don't think thats a stupid or trivial question, so why do people downvote it...? And so far I only see explanations of == noone answering the question if you can have two different references to the same Object. Edit: Trivially I would say you can't have two references to one object. But there are often more Options in a programming language than one would exspect. Like jumps in Java or these kind of things. – Mein Name Oct 11 '16 at 18:36
4 Answers
== checks if two references are identical.
In other words: if that check returns true, there is only one object.
If it returns false, there are two objects.
But of course, one can easily write some code where a != b; but a.foo() will have an effect on b.
Like:
class Bar {
private static int counter = 0;
void foo() { counter++ };
int getCounter() { return counter; }
When you now have:
Bar a = new Bar();
Bar b = new Bar();
then a != b; but still, when you do
System.out.println(b.getCounter());
a.foo();
System.out.println(b.getCounter());
will print 0 and then 1.
So in essence, the answer to your second question is: a != b does not imply that "doing something with a" does nothing to b".
But for the record - that last sentence is the reason why static can be seen as abnormality in good OO design; exactly for such "strange" side effects.
Edit to answer on "what happens with containers and clone"?
Of course, when you have something like:
List<WhatEver> a = some list containing one object "X"
List<WhatEver> b = a "clone" of a
Then a != b; but of course; when you call a.get(0).foo() that affects b.get(0) ... because both lists internally point to the same X
And you see: in that case, a.equals(b) would return true; whereas a == b is false.

- 137,827
- 25
- 176
- 248
-
Could this also be true if you e.g. clone an object containing other objects? Would not the objects inside the cloned object point to the same location? – Alexander Simko Oct 11 '16 at 18:33
-
@AlexanderSimko There is a difference between a shallow-copy and a deep-copy. With a shallow-copy objects inside the copied object will be the same. In a deep-copy the objects inside are copied as well. – puhlen Oct 11 '16 at 18:48
-
a static member belongs to the class and not to the instance (object). Changing the static value does not affect the both instances. – Heri Oct 11 '16 at 19:02
-
@Heri Sure, and in the real world, most people would avoid to write such code. But such code can be written; and therefore calling non-static methods on two different objects can have such stupid side effects. – GhostCat Oct 11 '16 at 19:09
-
Of course, correct code would have also the getter method static. But: OP asks if changing a object changes also b (if a!=b). This is definitly not the case, because changing a does NOT change b. – Heri Oct 11 '16 at 19:15
-
What does "changing an object" actually mean?! You see, that wording alone is unclear, and allows for many things. What if "a" is a singleton cache structure; and "b" is about fetching statistical data from that singleton? Than a change like "a.clearCache()" might very much affect what method calls on b might return. The whole point is: there can be relationships between different objects; and my static-class example is just one example of that. – GhostCat Oct 11 '16 at 19:24
Firstly – object1
and object2
are not objects themselves, they each are variables that hold reference to an object (unless null). So, ==
does not check if two objects are in the same memory location (as this can't possibly happen), but checks whether two variables hold reference to the same object. Therefore if object1 != object2, the two variables do not contain a reference to the same object.

- 96
- 1
- 4
I think it would be easier to understand with an example, the best example IMHO is when you override equals, when you create instances of Human with a name and a age, the method equals reads:
- If (e.g John) is the same as John, then both humans are equal (a clone of John)
- If the Object passed is not human then return false, they are not the same ( e.g a dog, who knows)
Then check field by field if the person is the same, there could be another John with the same age, even though they are different humans.
public class Human { private final String name; private final int age; public Human(String name, int age) { this.name = name; this.age = age; } public String getName() { return this.name; } public int getAge() { return this.age; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Human)) { return false; } Human that = (Human) o; if (this.age != that.age) return false; return this.name != null ? this.name.equals(that.name) : that.name == null; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; } }

- 400
- 1
- 3
- 18