The equals method:
Every object in Java has a method whose name is equals. The reason is that Object has a public method of this name and every class inherits from Object, either directly or indirectly. The header for this method is as follows:
public boolean equals(Object obj)
However, Object's implementation of equals returns true if its Object type argument is the same reference as the receiver object reference. So the inherited equals method returns true if the argument to equals has the same reference as the object on which this method is invoked.
This method is provided so that all objects can respond to an invocation of an equals method, but it is expected that programmers will override this method so that its behaviour makes sense for its class. When performing tests for equality of two objects, you must remember that a reference is not an object, and a reference value (presuming it is not null) represents the location of an object in memory. Comparing two references for equality does not compare the contents of the objects referenced. This is why, if we need to compare objects of a class, it is necessary to write a method.
We will take the User class as an example. We originally defined objects of the class User to have three instance variables: a string representing a user ID, a string representing an email address, and an integer representing a number of accesses. We wrote a two-argument constructor to initialize the data:
public User (String anID, String anAddress)
{
numOfAccesses = 0;
userID = anID;
emailAddress = anAddress;
}
When we write the code:
User emma = new User("Cornes", "[email protected]");
User maria = new User("Cornes", "[email protected]");
we create two User references and two User objects. These objects in turn contain references to String objects. A picture of the object referenced by emma is shown in Figure.
Consider what happens when we do the following test:
if (emma == maria)
{
System.out.println("Same user");
}
else
{
System.out.println("Different user");
}
The output of this piece of code would be "Different user". This is not because the objects referenced by emma and maria contain different data (although they do), but because the test compares only the values stored in the references for equality, not the values stored in the objects referenced. At some point we might write: maria = emma; This would copy the reference stored in
emma to the reference variable maria. In this case, the output of the if statement would be "Same user" because the value stored
in each of these references is now the same. (The object formerly referenced by maria might be garbage collected.)
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your The equals method homework and assignments? Live The equals method experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The equals method homework help, java assignment help and The equals method projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours