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
How do you take thread dump in java?
Can we create an object if a class doesn't have any constructor ( not even the default provided by constructor ) ?
What are the different types of inheritance in java?
What is parsing in java?
What is the unit of plancks constant?
What are java packages?
Why do people says “java is robust”?
What is multi-catch block in java?
How variables are declared?
What is flag in java?
What is thread safe java?
What are the classes of java?
Which collection is sorted in java?
Explain the overview of UDP messaging.
What are the important features of Java 10 release?