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
What is difference between throw and throws ?
what is thread? What are the high-level thread states? Or what are the states associated in the thread? : Java thread
What is n in java?
What happens when a thrown exception is not handled?
Will minecraft java be discontinued?
What are the application of stack?
What is the size of int?
Why arraylist is used in java?
What is the difference between compiler and jvm?
What are the important features of Java 9 release?
Explain method local inner classes ?
What type of variable is gender?
How listener identify that the event came from a particular object?
Can inner class be public in java?
What is object class in java?