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 / prachi

Equals() method of Qbject class checks whether reference id's are same or not.if overridden then it will check for the value contained in those reference id's.String class has overridden equals() so it checks for the value whereas Employee class hasn't so Object class' method runs and reference id's are compared.

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is time complexity algorithm?

545


What is the difference between menuitem and checkboxmenu item?

771


What is the purpose of using break in each case of switch statement?

559


how we can make a write-only class in java?

558


Explain different states of a thread in java?

581






Is zero a natural number?

561


What is multi-catch block in java?

613


What if static is removed from main method?

566


How to split arraylist elements in java?

587


Can constructor return value?

509


What about anonymous inner classes in java?

559


What is isa relationship?

559


What are parsers? Dom vs sax parser.

555


What is difference between hashset and hashmap?

554


What is Java Reflection API? Why it’s so important to have?

599