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 the difference between abstraction and encapsulation?
how to handle exceptions in ejb?
Which one will take more memory: an int or integer?
Can we define a package statement after the import statement in java?
Which is the best approach for creating thread ?
What is an empirical question?
Difference between stack and queue?
What kind of variables can a class consist?
What does math floor () do?
What is the difference between error and an exception?
How will you add panel to a frame?
What is the parse method in java?
Why singleton pattern is better than creating singleton class with static instance?
What is meant by stack and queue?
What are heap memory and stack memory and what are memory tables.