If I will write
String s=new String("XYZ");
String s1=new String("XYZ");
if(s.equals(s1)){
sop("True");
}
else{
sop("False");
}
This program will give me "True".
But When I am creating my own class suppose
class Employee{
public Employee(String name);
}
Employee e= new Employee("XYZ");
Employee e1 = neew Employee("XYZ");
if(e.equals(e1)){
sop("True");
}
else{
sop("False");
}
Then it will give the output as "False".
Can I know what is happening internally?
Answer Posted / vikas
Extending Ashish Singh's answer, If you want employee to return true for the use case you have mentioned, override your equals and hashcode method and check only for the String object that you have in employee class in your overridden equals method. It'll return true.
Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
How garbage collection is done in java?
What are basic keywords?
How do you delete a list in java?
What does replaceall do in java?
What are advantages and disadvantages of OOPs?
How do you sing an Applet ?
Is string serializable in java?
why doesn't java run on all platforms?
How is hashset defined in java?
What are the methods of object class ?
What are the advantages of functions?
What is regex in java?
What is final keyword in java? Give an example.
What’s the difference between callable and runnable?
What is java in simple terms?