The equals method Assignment Help

Assignment Help: >> Objects in Java >> The equals method

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. 

421_equals method.png

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

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd