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 difference between hashset and hashmap?
What is constructor and virtual function? Can we call a virtual function in a constructor?
What is the purpose of the system class in java?
What is nan in java?
How can you traverse a linked list in java?
How to instantiate member inner class?
What is an immutable class?
How do you add an arraylist to an array in java?
How to solve the problem of generating the unique hash keys with hash function?
Write a program to check for a prime number in java?
Can we define private and protected modifiers for the members in interfaces?
Are arrays static in java?
How do you do absolute value in java?
Explain a few methods of overloading best practices in java?
what do you understand by synchronization? Or what is synchronization and why is it important? Or describe synchronization in respect to multithreading? Or what is synchronization? : Java thread