Equals override for String and Int Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Equals override for String and Int without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a list in that list I created an object. By using the contains() method, I want to check whether the object already exists or not. For that, I override the equals() method. Everything is perfect upto this. But when I try to do the same thing for String and int the equals() override doesn’t not work. Why is it like this? I just posted some sample code for reference.

public class Test 
private int x;

public Test(int n) 
x = n;

public boolean equals(Object o) 
return false; 

public static void main(String[] args) 
List<Test> list = new ArrayList<Test>();
list.add(new Test(3));
System.out.println("Test Contains Object : " + list.contains(new Test(3))); // Prints always false (Equals override)
List<String> list1 = new ArrayList<String>();
String a = "Testing";
System.out.println("List1 Contains String : " + list1.contains(a)); // Prints true (Equals override not working)


String and Integer are both final classes, so you cannot subclass them. Therefore you cannot override their equals methods.

You can, however, subclass ArrayList and create your own contains implementation builds on the existing one.

We are here to answer your question about Equals override for String and Int - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji