-3
public class ObjectClass {
    public static void main(String[] args) {
        Demo dm = new Demo();
        Object obj = dm.getObject();
        System.out.println("Class name :: "+obj.getClass());
        System.out.println("To String " + dm.toString());
        System.out.println("HashCode "+ dm.hashCode());
    }
}

Output

    Class name :: class newTopic.Object.Demo
    To String :: newTopic.Object.Demo@2a139a55
    HashCode :: 705927765

What is the difference between that Demo@2a139a55 and hascode 705927765

Dhiraj
  • 1
  • 1
  • also: toString returns a "String representation" of the object. a hashCode is an int number associated with the object – Stultuske Dec 10 '20 at 07:04

2 Answers2

0

If you look at the Javadoc of Object's toString(), you'll see that:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', andt he unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Hence dm.toString() returned:

newTopic.Object.Demo      @           2a139a55

getClass().getName()  +  '@'  + Integer.toHexString(   705927765   )
                                                     dm.hashCode()
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Yes, thank you for the clarification. Actually I wasn't think in that way so I got confused. – Dhiraj Dec 11 '20 at 05:45
-1

These two are Object class method. If you do not override, then these will be inherited automatically. Here I will try to explain 3 methods, toString(), equals() and HashCode().

  1. toString - It help to represent the object in form of String. (for more info - https://www.geeksforgeeks.org/object-tostring-method-in-java/ go through this link).

  2. equals and HashCode - equals method is used to compare the equality of two object and HashCode is helpful to generate hashCode of the object. Both together are helpful in HashMap (mostly). To get more information, please read about internal implementation of HashMap. https://www.geeksforgeeks.org/internal-working-of-hashmap-java/

I hope this would be helpful for you.

Lokesh Sharma
  • 411
  • 5
  • 6
  • 1
    equals method is used to compare two methods. No, it isn't. it is used to check the equality of two objects. If not overridden, it will only return true, if a referential check (==) returns true, since this is the base implementation inherited from the Object class. "HashCode is helpful to generate hashCode of the class". No, it isn't. It's hashCode, and it doesn't create a code of the class, but a code for the specific instance of the class. If it's the same value for every instance of a class, the code is about as helpful as a burgerstand at a vegan convention. – Stultuske Dec 10 '20 at 07:42
  • Even then, stating that hashCode generates a hashCode ... doesn't really explain what it is useful for to generate hashCodes. – Stultuske Dec 10 '20 at 07:45
  • I think the links are very useful. This is not completely right answer, here just I shared some information and links. There you can read in detail. – Lokesh Sharma Dec 10 '20 at 11:27