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


Please Help Members By Posting Answers For Below Questions

How do you check if a string is lexicographically in java?

503


Differences between traditional programming language and object oriented programming language?

574


Does java arraylist maintain insertion order?

552


Mention the default values of all the elements of an array defined as an instance variable.

525


What is the size of int?

602






Which list does not allow duplicates in java?

516


Can we catch more than one exception in a single catch block?

631


What is replaceall in java?

547


Explain java thread life cycle.

583


What are the types of java languages?

519


What is a map in java?

555


What is the role of the java.rmi.naming class?

529


Can a static member function access member variable of an object?

564


What is substring 1 in java?

591


What is class forname?

548