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
What is Hierarchy of exception?
Describe string intern() methodology
What is the difference between a field variable and a local variable?
What is the difference between integer parseint and integer valueof?
How do I remove a character from a string in java?
What is final?
Can we override a variable in java?
What does 0 mean in boolean?
Differentiate between the constructors and methods in java?
What is a nullable field?
What is namespace in java?
What is the numeric promotion?
Explain the private field modifier?
What are the different data types in java?
What is qualitative variable?