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 the hashcode () and equals () used for?
What is a super method?
What loop means?
What is flag in java?
Can we overload the constructors?
How long will it take to learn java?
Can we create an object of static class in java?
What's the difference between int and integer in java?
Can an interface have a class?
List any five features of java?
What is difference between core java and java ee?
Explain list interface?
How do you test a method for an exception using junit?
How to create com object in Java?
What is operator overloading. Is it is supported in java?