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 check if a string is lexicographically in java?
Differences between traditional programming language and object oriented programming language?
Does java arraylist maintain insertion order?
Mention the default values of all the elements of an array defined as an instance variable.
What is the size of int?
Which list does not allow duplicates in java?
Can we catch more than one exception in a single catch block?
What is replaceall in java?
Explain java thread life cycle.
What are the types of java languages?
What is a map in java?
What is the role of the java.rmi.naming class?
Can a static member function access member variable of an object?
What is substring 1 in java?
What is class forname?