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
Why do we need singleton?
Explain about features of local inner class?
How does varargs work in java?
What is the difference between C++ and Java and your preferences?
What is a treeset in java?
What is the final keyword?
What is a protected void?
What are different types of control structures?
Is array primitive data type in java?
Can we override static methods in java?
What is the difference between abstract class and interface1? What is an interface?
When do I need to use reflection feature in java?
How do you achieve singleton?
What is __ init __ functions?
What about member inner classes?