Junior — Middle
What standard methods, besides toString, are you familiar with or usually use?
sobes.tech AI
Answer from AI
Besides the toString() method, the following standard methods are often used in Java, defined in the Object class and other base classes:
equals(Object obj)— for comparing objects for equality based on content.hashCode()— returns the hash code of the object, used in collections likeHashMap.clone()— creates a copy of the object (with proper implementation of theCloneableinterface).finalize()— called before garbage collection (deprecated, not recommended for use).
Standard classes also frequently use methods such as:
List.add(),List.remove()— for working with collections.String.equals(),String.substring(),String.split()— for string operations.
Example of using equals and hashCode:
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
MyClass other = (MyClass) obj;
return this.id == other.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}