D. Không xây dựng, cho phép các nhà xây dựng mặc định 16. Mà hai trong số các báo cáo này là đúng sự thật về xây dựng? (Chọn hai) A. Các nhà xây dựng không phải có lập luận nếu các nhà xây dựng lớp cha không có đối số. Các nhà xây dựng B. không được thừa kế. Các nhà xây dựng C. không có thể bị quá tải. D. Câu lệnh đầu tiên của | Overriding hashCode and equals Exam Objective 9 don t follow the contract you may still compile and run but your code or someone else s may break at runtime in some unexpected way. The equals Contract Pulled straight from the Java docs the equals contract says It is reflexive For any reference value x x should return true. It is symmetric For any reference values x and y y should return true if and only x returns true. It is transitive For any reference values x y and z if y returns true and z returns true then z should return true. It is consistent For any reference values x and y multiple invocations of y consistently return true or consistently return false provided no information used in equals comparisons on the object is modified. For any nonnull reference value x null should return false. And you re so not off the hook yet. We haven t looked at the hashCode method but equals and hashCode are bound together by a joint contract that specifies if two objects are considered equal using the equals method then they must have identical hashcode values. So to be truly safe your rule of thumb should be if you override equals override hashCode as well. So let s switch over to hashCode and see how that method ties in to equals . Overriding hashCode The hashcode value of an object is used by some collection classes we ll look at the collections later in this chapter . Although you can think of it as kind of an object ID number it isn t necessarily unique. Collections such as HashMap and HashSet use the hashcode value of an object to determine where the object should be stored in the collection and the hashcode is used again to help locate the object in the collection. For the exam you do not need to understand the deep details of how the collection classes that use hashing are implemented but you do need to know which collections use them but um they all have hash in the name so you should be