Comparing objects?

Is there any method that allows you to compare objects to see if they are equal???

wazilaAsked:
Who is Participating?
 
mwibbelsConnect With a Mentor Commented:
Every object has a method equals(Object o) which checks if two objects are equal, so a.equals(b) returns true if a equals b. You can override this method (as has been done in the String class) to let your own classes compare.

Keep in mind that, for Strings a and b, the expression "a.equals(b)" is not the same as "a==b": the first compares the contents of two strings, the second compares their references.

Most overrides of "equals" first check if both objects are instances of the same class and then compare their contents.

Also check the Object and String documentation of the "equals" method.
0
All Courses

From novice to tech pro — start learning today.