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
Is multiple inheritance supported by java?
How to optimize the javac output?
Why are constructors used?
What is double in java?
What are different types of classloaders?
What is udp in java?
Are there structures in java?
What is java developer skills?
What is boolean example?
I want to print “hello” even before main is executed. How will you acheive that?
How destructors are defined in java?
Can we force garbage collector to run ?
How do listeners work?
What is lossy conversion in java?
When should the method invokelater() be used?