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
Explain the importance of finalize() method.
What are the classes of java?
What is mnemonic code?
Difference between object and reference?
State the difference between strings and arrays.
What is a numeric format?
What are methods of a class?
How do you write methodology?
What is the difference between preemptive scheduling and time slicing in java programming?
How many bits are used to represent unicode, ascii, utf-16, and utf-8 characters?
What is return data type?
What is the benefit of inner / nested classes ?
What is bool mean?
What is a class in java?
What is %d in printf?