How can I compare class objects for equality?

  • Thread starter Thread starter russel.arnold
  • Start date Start date
  • Tags Tags
    Class
Click For Summary
SUMMARY

To compare class objects for equality in Java, it is essential to override the equals() method. This method should compare the fields of the objects, specifically the two strings and one integer, to determine if they are equal. The discussion emphasizes that using a loop is not a suitable approach for this task. Instead, implementing a manual comparison by overriding the equality operator is the recommended method.

PREREQUISITES
  • Understanding of Java class structure and object-oriented programming
  • Knowledge of Java's equals() method and its implementation
  • Familiarity with string comparison in Java
  • Basic understanding of Java data types, specifically String and int
NEXT STEPS
  • Research how to override the equals() method in Java
  • Learn about the hashCode() method and its relationship with equals()
  • Explore best practices for comparing objects in Java
  • Investigate the use of Java's Objects.equals() for null-safe comparisons
USEFUL FOR

Java developers, software engineers, and anyone involved in object-oriented programming who needs to implement custom equality checks for class instances.

russel.arnold
Messages
38
Reaction score
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
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.
 
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.
 

Similar threads

  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K