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
Give me some null interfaces in java?
What is the difference between access specifiers and access modifiers in java? 16
What do you understand by the term wrapper classes?
What are methods of a class?
Write a program to reverse a number in java?
Why can we not override static method?
Can an interface implement another interface?
How to declare an arraylist in java?
How does multithreading take place on a computer with a single cpu?
what is the role of xml in core java?? and how we can use it?? can somebody give a sample program with explanation and from where i can read more about xml?????
Can the interface be final?
What is an image buffer?
How to pass arraylist to stored procedure in java?
Can we declare a class as abstract without having any abstract method?
what is meant by abstract class?