4

I know that this.toString() does not print the address of the object but the hash code. And I have read discussions like this one as well

But, lets say this.toString() prints the same value on 2 occasions and a different value on the 3rd occasion. Then, can I conclude that the object on the first 2 occasions was the same and the third occasion was different?

I am trying to determine(only for testing) if an object is the same one or a different one. Like say this points to same address or different.

Any better ways of checking if an object is the same or different one in Java running on Android devices? Or if I could get the address of the object in some way then the whole dilemma is resolved.

AdeleGoldberg
  • 1,289
  • 3
  • 12
  • 28
  • 2
    `toString` can be implemented in any possible way, so you'll never be able to get *reliable* information from it in the general sense. If you only care about specific objects with a known `toString()` implementation, then you might get somewhere ... but not in the general case. Also note that getting the address wouldn't even fully solve the problem, because an address could be re-used once the original object was GCed. – Joachim Sauer Oct 14 '19 at 15:37

3 Answers3

4

There are three main ways to compare two objects in Java:

  1. Compare object references (the one you're looking for)

    obj1 == obj2
    

    This condition will be true if and only if both references are pointing to the same object.

    CustomType obj1 = new CustomType();
    CustomType obj2 = ob1;
    boolean thisIsTrue = obj1 == obj2;
    

    but:

    CustomType obj1 = new CustomType();
    CustomType obj2 = new CustomType();
    boolean thisIsFalse = obj1 == obj2;
    
  2. Compare object data

    obj1.equals(obj2)
    

    The result of this condition depends on actual implementation of equals method on obj1's class. E.g. this poorly designed equals method will always return false. Even if obj1 and obj2 are pointing to the same object.

    @Override
    public boolean equals(Object other){
        return false;
    }
    
    CustomType obj1 = new CustomType();
    CustomType obj2 = ob1;
    boolean thisIsTrue = obj1 == obj2;
    boolean thisIsFalse = obj1.equals(obj2);
    
  3. Compare object hash-codes

    obj1.hashCode() == obj2.hashCode()
    

    The result of this condition depends on actual implementation of hashCode method on both obj1's and obj2's classes. Using this approach is not encouraged as it is even less trustable. Two completely different objects can have the same hash-code. It should be used providently and cautiously in some specific cases.

ETO
  • 6,970
  • 1
  • 20
  • 37
2

If you want to check the equality of message you should use equals method available in object class, which is super class to all classes in Java by default.

Let's you want to check for Object Mapper. You can do it like below:

Mapper mapper = new Mapper();
Mapper mapper1 = mapper;
Mapper mapper2 = new Mapper();
mapper.equals(mapper1); //true
mapper.equals(mapper2); //false

There can be two objects with same hashcode with still they can be different. If you want some custom equal checking logic, then you should override hashcode and equals method of Object class in your Class.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
2

The easiest way to determine if an object reference points to the same instance as an other reference is to use == operator:

yourRef1 == yourRef2

Alternatively, if you want to know the immutable reference of an object, you can use System.identityHashCode(yourObject):

Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode(). The hash code for the null reference is zero.

matdev
  • 4,115
  • 6
  • 35
  • 56