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
What is time complexity algorithm?
What is the difference between menuitem and checkboxmenu item?
What is the purpose of using break in each case of switch statement?
how we can make a write-only class in java?
Explain different states of a thread in java?
Is zero a natural number?
What is multi-catch block in java?
What if static is removed from main method?
How to split arraylist elements in java?
Can constructor return value?
What about anonymous inner classes in java?
What is isa relationship?
What are parsers? Dom vs sax parser.
What is difference between hashset and hashmap?
What is Java Reflection API? Why it’s so important to have?