Explain the equals() method, JAVA Programming

Assignment Help:

Explain the equals() method

The equals() method of java.lang.Object acts the similar as the == operator; that is, it tests for object identity rather than object equality. The implicit contract of the equals() method, thus, is that it tests for equality rather than identity. Therefore most classes will override equals() along with a version that does field through field comparisons before deciding while to return true or false.

To elaborate, an object created through a clone() method (that is a copy of the object) should pass the equals() test if neither the original nor the clone has changed because the clone was created. Therefore the clone will fail to be == to the original object.

For instance, here is an equals() method you could use for the Car class. Two cars are equal if and only if their license plates are equal, and that's what this techniques tests for.
public boolean equals(Object o) {

if (o instanceof Car) {
Car c = (Car) o;
if (this.licensePlate.equals(c.licensePlate)) return true;
}
return false;
}
This example is particularly interesting since it demonstrates the impossibility of writing a useful generic equals() method in which tests equality for any object. It is not sufficient to easily test for equality of all the fields of two objects. It is whole possible that some of the fields might not be relevant to the test for equality as in this instance where changing the speed of a car does not change the in fact car that's referred to.

Be careful to prevent this common mistake when writing equals() methods:
public boolean equals(Car c) {

if (o instanceof Car) {
Car c = (Car) o;
if (this.licensePlate.equals(c.licensePlate)) return true;
}
return false;

}
The equals() method must permit tests against any object of any class, not simply against other objects of the similar class (Car in this example.)

You do not requires to test whether o is null. null is never an instance of any class. null instanceof Object returns false.


Related Discussions:- Explain the equals() method

Illustrate inheritance and composition?, The ' is a ' relationship is use...

The ' is a ' relationship is used with inheritance and ' has a ' relationship is used with composition . Both composition and inheritance allow you to perform sub-objects in

Padovan series, write a program in java using array and scanner class to ge...

write a program in java using array and scanner class to generate padovan series

Java identifiers, 1. Which of the following are not valid Java identifiers,...

1. Which of the following are not valid Java identifiers, and why? (a) wolVes (b) United(there is only one) (c) 87 (d) 5 3 (e) Real ale (f) isFound?by 2. A cla

Give a example of using tostring() methods, Give a example of Using toStrin...

Give a example of Using toString() Methods ? Below is a version of CarTest in which uses toString() and System.out.println() instead of printing the fields in a straight line

Garbage collection guarantee, Is it right that garbage collection guarante...

Is it right that garbage collection guarantee that a program will not run out of memory?

Describe the if statement in java, Describe the if statement in Java ? ...

Describe the if statement in Java ? All but the most trivial computer programs required to form decisions. They test a condition and operate in a different way based on the out

Concurrent Programming, Problem 1 A savings account object holds a non-nega...

Problem 1 A savings account object holds a non-negative balance, and provides deposit(k ) and withdraw(k ) methods, where deposit(k ) adds k to the balance, and withdraw(k ) subtra

Why we avoiding short circuits, Why we Avoiding Short Circuits ? If you...

Why we Avoiding Short Circuits ? If you need all of your boolean expressions evaluated regardless of the truth value of each, then you can use & and | instead of && and ||. The

Write Your Message!

Captcha
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