0% found this document useful (0 votes)
31 views1 page

Discussion Forum Unit 4 Cs 2203

In Java, all classes inherit from the Object class, which contains methods like equals() and ==. The equals() method compares the values of objects, while == compares the locations in memory. equals() is commonly used to compare objects and is useful when dealing with null values, but it can be difficult to use if comparing objects based on specific criteria like sort order. The == operator or compareTo() method may be better alternatives depending on the specific comparison needs.

Uploaded by

Julio Louzano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views1 page

Discussion Forum Unit 4 Cs 2203

In Java, all classes inherit from the Object class, which contains methods like equals() and ==. The equals() method compares the values of objects, while == compares the locations in memory. equals() is commonly used to compare objects and is useful when dealing with null values, but it can be difficult to use if comparing objects based on specific criteria like sort order. The == operator or compareTo() method may be better alternatives depending on the specific comparison needs.

Uploaded by

Julio Louzano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

In java, all classes inherit the Object class directly or indirectly (Eck, 2021).

The object class


contains methods like toString(), hashCode(), getClass, clone(), equals() and many more. The
equals() method is used to compare the values of objects and not the location of the objects in
the memory. The "==" is used to compare the location of the objects.

The advantages of using the equals() method are as follow;

The polymorphism property of object-oriented programming makes it easier to compare two


instances of objects since they all inherit the equals() method from the Object class. Also the
equals() method is useful when dealing with null values over the "==". Whenever the equals()
method is applied to an object with a null value, nullpointerexception occurs which makes it
easy to debug.

The disadvantage of using the equals() method is that ;

Checking the equality of objects among certain criteria makes it difficult to use the equals()
method. For example, if we are to compare sorted objects to know which comes first then we
need to override the equals() method since there is a natural notion of ascending order for
arbitrary objects (Eck, 2021).

The equals() method is commonly used by programmers and I think is good for comparing two
values of objects but when one needs to compare the locations of the objects in the memory
then the alternate is the "==". Also, when one needs to compare two objects based on criteria
such as which object comes first then the compareTo() method is a good alternative for that.

Reference

Eck, D. J. (2021). Introduction to programming using java (8th ed.).


http://math.hws.edu/javanotes/

You might also like