-5

I have two java objects that are instantiated from the same class.

Ob x = new Ob( );
Ob z = x;

Since it is of the same type, is it not supposed to execute the statements in the 'if' part?

if (x == z) {
  //Do something
}
else {
   //Do something else
}
pk.
  • 99
  • 3
  • 12
  • The way you phrased the question makes it sounds like you have code which is not working the way (you think) it should. If this is the case, you should post a [mcve] reproducing your issue. – Bernhard Barker Sep 14 '15 at 13:56
  • 2
    `I have two java objects`, nope. You have one javaobject in two variables. – Grim Sep 14 '15 at 13:57

2 Answers2

4

Yes.

In java, two objects are considered equal using the == operator if and only if they point to the same location in memory i.e. they have the same reference.

James Wierzba
  • 16,176
  • 14
  • 79
  • 120
3

Ob z = x; means that you only have one object with two variables referring to it. Therefore x == z is true.

Eran
  • 387,369
  • 54
  • 702
  • 768