Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Comparing objects?

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

0
wazila
Asked:
wazila
1 Solution
 
mwibbelsCommented:
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now