Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
How It Works
The comparison operator (==) can determine the equality of two objects. This equality does not pertain to the object values but rather to the object references. Often an application is more concerned with the values of objects; in such cases, the equals() method is the preferred choice because it compares the values contained within the objects rather than the object references. The comparison operator looks at the object reference and determines whether it points to the same object as the object reference that it is being compared against. A Boolean true result is returned if the two objects are equal; otherwise, a Boolean false result is returned. In solution 1, the first comparison between the team1 object reference and the team2 object reference returns a false value because those two objects are separate in memory, even though they contain the same values. The second comparison in solution 1 between the team3 object reference and the team4 object reference returns a true value because both of those references refer to the team1 object. The equals() method tests whether two objects contain the same values. To use the equals() method for comparison, the object that is being compared should override the Object class equals() and hashCode() methods. The equals() method should implement a comparison against the values contained within the object that would yield a true comparison result. The following code is an example of an overridden equals() method that has been placed into the Team object. @Override public boolean equals(Object obj) { if (this == obj) { return true; Chapter 5 ObjeCt-Oriented java 193 } if (obj instanceof Team) { Team other = (Team) obj; return other.getName().equals(this.getName()) && other.getCity().equals(this.getCity()) && other.getPlayers().equals(this.getPlayers()); } else { return false; } } As you can see, the overridden equals() method first checks whether the object that is passed as an argument is referencing the same object as the one it is being compared against. If so, a true result is returned. If both objects are not referencing the same object in memory, the equals() method checks whether the fields are equal. In this case, any two Team objects that contain the same values within the name and city fields would be considered equal. Once the equals() method has been overridden, the comparison of the two objects can be performed, as demonstrated in solution 2 to this recipe. The hashCode() method returns an int value that consistently returns the same integer. There are many ways in which to calculate the hashCode of an object. Perform a web search on the topic, and you find various techniques. One of the most basic ways to implement the hashCode() method is to concatenate all the object’s variables into string format and return the resulting string’s hashCode(). It is a good idea to cache the value of the hashCode for later use because the initial calculation may take some time. The hashCode() method in solution 2 demonstrates this tactic. Comparing Java objects can become confusing, considering that there are multiple ways to do it. If the comparison you want to perform is against the object identity, use the comparison (==) operator. However, if you want to compare the values within the objects, or the state of the objects, then the equals() method is the way to go. Chapter 5 ObjeCt-Oriented java |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling