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 are the types of methodology?
What is the escape character in java?
What is ctrl m character?
What is boolean query?
What is the difference between private & public & friendly classes?
What are the special characters?
How to implement an arraylist in java?
What is the common usage of serialization?
What is vector?
What does += mean in java?
What is mnemonic in assembly language?
What’s the difference between callable and runnable?
How are multiple inheritances done in Java?
What is lastindexof in java?
What is the disadvantage of java?