27 questions for “equals()”

Found in 1 field. Select a question to see its full answer.

Information Technology

27 questions in 2 technologies

#QuestionTechnologyTopicLevel
1What is the default behavior of equals() and hashCode() inherited from Object, and why is it usually necessary to override them for value-based classes? Java › equals(), hashCode() & toString() ContractsJavaequals(), hashCode() & toString() ContractsBeginner
2What are the five formal properties an equals() implementation must satisfy according to its documented contract (reflexive, symmetric, transitive, consistent, and null-handling)? Java › equals(), hashCode() & toString() ContractsJavaequals(), hashCode() & toString() ContractsIntermediate
3How does Objects.equals() and Objects.hash() from java.util.Objects simplify writing correct equals() and hashCode() implementations, particularly regarding null-safety? Java › equals(), hashCode() & toString() ContractsJavaequals(), hashCode() & toString() ContractsIntermediate
4What is the relationship between equals() and the == operator when comparing primitive types versus reference types, and how does this affect comparing boxed wrapper types like Integer? Java › equals(), hashCode() & toString() ContractsJavaequals(), hashCode() & toString() ContractsIntermediate
5How would you write a unit test that thoroughly verifies a custom equals() and hashCode() implementation satisfies its contract, and what testing libraries exist specifically for this purpose? Java › equals(), hashCode() & toString() ContractsJavaequals(), hashCode() & toString() ContractsIntermediate
6How does Comparable's compareTo() method relate to equals(), and what does it mean for compareTo() to be 'consistent with equals'? Java › equals(), hashCode() & toString() ContractsJavaequals(), hashCode() & toString() ContractsIntermediate
7Why is it particularly important for a class's equals() and hashCode() to be based on effectively immutable data specifically when that class is used as a JPA/Hibernate entity, and what special considerations apply to entity identity before it's persisted (has a generated ID)? Java › equals(), hashCode() & toString() ContractsJavaequals(), hashCode() & toString() ContractsIntermediate
8How does the getClass() versus instanceof choice in an equals() implementation affect whether subclasses can be considered equal to their superclass instances, and what's the Liskov Substitution Principle tension involved? Java › equals(), hashCode() & toString() ContractsJavaequals(), hashCode() & toString() ContractsAdvanced
9Why must hashCode() be overridden whenever equals() is overridden, and what specific, hard-to-diagnose bugs occur in a HashMap when this contract is violated (equals() overridden but hashCode() left as default)? Java › equals(), hashCode() & toString() ContractsJavaequals(), hashCode() & toString() ContractsAdvanced
10How does Java Record's automatically-generated equals(), hashCode(), and toString() implementations work, and in what specific ways do they differ from a manually-written, field-by-field implementation? Java › equals(), hashCode() & toString() ContractsJavaequals(), hashCode() & toString() ContractsAdvanced