You are mixing up different ideas.
One concept is known as identity: are two references pointing to the same object, that is, the same chunk of memory? This is what ==
tests, as in x == y
where x and y are reference variables (pointers) rather than primitives.
The .equals
method on the Object
class has a different intention. That method asks if the meaning of two objects is the same. The method judges if the content of two objects are significantly the same. In some cases “significantly” means exactly the same for every value of every field. What that means exactly is up to the particular subclass inheriting from Object
. Whoever writes the implementation of equals
gets to decide on what should be compared between the two objects.
As for comparing binary streams, first of all, objects don’t have a binary stream. Furthermore, even two pieces of text can have the same meaning yet vary in their bits – just look up Unicode Normalization. Or even the simpler case where in some situations two pieces of text might be considered equivalent if we choose to ignore uppercase versus lowercase, ex: Dog
and dog
and DOG
might all three be considered that same while each carries different octets.
Context is everything.
In java, when you compare object1 and object2 using Object1.equals(object2), they are not equal even though the content of the object is same.
Your statement simply cannot be made about all classes. You must look to each class’ implementation of equals
. Class Foo
might compare every last piece of state in the object, while class Bar
might choose to compare only a single id
field of type UUID
while ignoring the value in all the rest of its fields.
Assert.assertEquals(j1,j2): assertion fails. why ?
I do not know. I’d have to look up the Javadoc for the implementation of the Object::equals
method override for the particular class in question (JSONObject
in your case).
The point is that you must look up the equals
method documentation for each comparison you make.
Likewise, be sure to write clear documentation for every equals
method override you implement.