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


Please Help Members By Posting Answers For Below Questions

How garbage collection is done in java?

546


What are basic keywords?

555


How do you delete a list in java?

532


What does replaceall do in java?

484


What are advantages and disadvantages of OOPs?

625






How do you sing an Applet ?

2028


Is string serializable in java?

538


why doesn't java run on all platforms?

565


How is hashset defined in java?

517


What are the methods of object class ?

560


What are the advantages of functions?

517


What is regex in java?

527


What is final keyword in java? Give an example.

574


What’s the difference between callable and runnable?

543


What is java in simple terms?

553