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

in the first case equals method compares the sequence of characters pointed by s and s1. as both are same it returns true. in the second case it is comparing the references i.e addresses of objects e and e1. as both are not same, it returns false.

Is This Answer Correct ?    11 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is difference between throw and throws ?

610


what is thread? What are the high-level thread states? Or what are the states associated in the thread? : Java thread

504


What is n in java?

542


What happens when a thrown exception is not handled?

584


Will minecraft java be discontinued?

655






What are the application of stack?

509


What is the size of int?

603


Why arraylist is used in java?

556


What is the difference between compiler and jvm?

559


What are the important features of Java 9 release?

540


Explain method local inner classes ?

597


What type of variable is gender?

552


How listener identify that the event came from a particular object?

1476


Can inner class be public in java?

557


What is object class in java?

507