-
Notifications
You must be signed in to change notification settings - Fork 3
Object methods
##About Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
But what emthods? Let's try to understand:
Methods:
Object
in Java has:
.toString()
.clone()
.hashCode()
.wait()
.notify()
.notifyAll()
.equals()
.finalize()
.getClass()
But what each method do? And why these so important?
Ofcourse, we want to have string representation of our object, that's why we have toString() in every object.
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
As we can see by default string representation has name of the class and it's hashCode in hexademical representation ,separated by "@". If it's necessary - we can override this method as we want. For example if we have class Person with name and age fields - we can write toString which return String with name and age. When we try to print our class - we use toString too.
In JavaDoc it's recommended that akk subclasses override this method
todo