How can I compare class objects for equality?

  • Thread starter russel.arnold
  • Start date
  • Tags
    Class
In summary, the conversation discusses creating a class with string and int arguments, and how to compare different objects of this class for equality. The speaker suggests overriding the equality operator and manually comparing each field of the objects. A loop is not a suitable method for this task.
  • #1
russel.arnold
41
0
Hi,

I have a class, say myclass and it takes two strings (let s and g ) and one int ( say i) as its arguments.

It looks like, public class {

String s , g;
int i ;

public void myclass(String sname, String gname, int k){

s = sname;
g = gname;
i = k;
}
}

Now, I have several objects of this class with different values of s, g and i. I want to compare them for equality. i.e if two class objects have same s g and i value, then i want true as my output.

I know this can be done by a loop but is there any shorter way to do this?
 
Technology news on Phys.org
  • #2
russel.arnold said:
Hi,

I have a class, say myclass and it takes two strings (let s and g ) and one int ( say i) as its arguments.

It looks like, public class {

String s , g;
int i ;

public void myclass(String sname, String gname, int k){

s = sname;
g = gname;
i = k;
}
}

Now, I have several objects of this class with different values of s, g and i. I want to compare them for equality. i.e if two class objects have same s g and i value, then i want true as my output.

I know this can be done by a loop but is there any shorter way to do this?

I'm pretty sure object equality has to be done manually by overriding the equality operator and comparing each field of the two objects.
 
  • #3
russel.arnold said:
Now, I have several objects of this class with different values of s, g and i. I want to compare them for equality. i.e if two class objects have same s g and i value, then i want true as my output.

I know this can be done by a loop but is there any shorter way to do this?
A loop isn't appropriate at all. There is no way you can write a loop that will compare two strings and an int in one class instance with variables of the same types in another class instance. You will need to do a comparison in the way that DavidSnider suggests.
 

1. How do I compare two class objects?

To compare two class objects, you can use the equals() method which is inherited from the Object class. This method compares the values of two objects and returns a boolean value indicating whether they are equal or not.

2. What is the difference between == and equals() when comparing class objects?

The == operator compares the memory addresses of two objects, while the equals() method compares the values of the objects. This means that two objects can have the same values but different memory addresses, resulting in == returning false but equals() returning true.

3. Can I customize the comparison of class objects?

Yes, you can customize the comparison of class objects by overriding the equals() method in your class. This allows you to define your own criteria for determining whether two objects are equal.

4. How can I compare class objects for sorting?

To compare class objects for sorting, you can implement the Comparable interface and override the compareTo() method. This method defines the sorting criteria for your class objects and allows them to be sorted in a collection using Collections.sort() or Arrays.sort().

5. What happens if my class does not have an equals() method?

If your class does not have an equals() method, it will inherit the equals() method from the Object class. This method compares the memory addresses of the objects, so it may not provide the desired comparison for your class. It is recommended to override the equals() method in your class for proper comparison.

Similar threads

  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
4
Views
866
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
522
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
3
Views
742
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top