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 / vikas

Extending Ashish Singh's answer, If you want employee to return true for the use case you have mentioned, override your equals and hashcode method and check only for the String object that you have in employee class in your overridden equals method. It'll return true.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the importance of finalize() method.

571


What are the classes of java?

543


What is mnemonic code?

544


Difference between object and reference?

690


State the difference between strings and arrays.

605






What is a numeric format?

574


What are methods of a class?

544


How do you write methodology?

566


What is the difference between preemptive scheduling and time slicing in java programming?

542


How many bits are used to represent unicode, ascii, utf-16, and utf-8 characters?

590


What is return data type?

563


What is the benefit of inner / nested classes ?

529


What is bool mean?

575


What is a class in java?

584


What is %d in printf?

534