why the equals method can be override?when we override the
equals method?

Answer Posted / ganesh

We need to override the equals method when we write our own class and we want the instance of that class to be part of some collection.

When you place the object in collection and make the manipulation on them, say sorting/searching etc.
The comparison of the object has to be done by the alogorithm.
The Algorithm uses the equals method to check for equality of two objects. Otherwise it wont be having any means to check for equality.

class Student
{
private String serialNo;
private String name;
}

two student objects with the same name cannot be the same students, but two student objects with the same serial number should the same object.

search(Student st,List a)
{
Collections.binarySearch(a,st);

}

when you want to put the objects in the hashMap, then overriding the cashCode is necessary. HashMap calls the hashCode method on the object to insert into HashMap.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we overload the constructors?

563


Do we have pointers in java?

538


What is a classloader in java?

538


What is a function argument in java?

522


What is proper subset?

537






What is an empty class? What functionality does it offer in Java?

679


What is meant by tab pans?

652


what is server side caching?

1612


Can you sort a string in java?

497


How we can execute any code even before main method?

555


What modifiers are allowed for methods in an interface?

654


What is java algorithm?

525


public class Test { public static void main(String[] args) { int countA = 0; int countB = 0; int countC = 0; int countD = 0; int countE = 0; int countF = 0; int countG = 0; int countH = 0; int countI = 0; int countJ = 0; int countK = 0; int countL = 0; int countM = 0; int countN = 0; int countO = 0; int countP = 0; int countQ = 0; int countR = 0; int countS = 0; int countT = 0; int countU = 0; int countV = 0; int countW = 0; int countX = 0; int countY = 0; int countZ = 0; } } Can anybody tell me any alternate solution(like loop or something) to automate this initialization process. Ex:- for(char chr='A';chr<='Z'; chr++) { (int) String "count"+chr = 0; }

1852


Explain with example the concept of constant variable in java.

640


What is the best definition for data?

523