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
Why webdriver is an interface?
Explain about anonymous inner classes in java?
Why is stringbuffer called mutable?
Is array serializable java?
Who is founder of java?
what is the difference between preemptive scheduling and time slicing? : Java thread
Is it necessary for the port addresses to be unique? Explain with reason.
when to use ArrayList and when to use HashMap in webApplication.
Can we use string in the switch case?
What is the difference between a local variable and an instance variable?
What is an infinite loop?
What is not object oriented programming?
For class CFoo { }; what default methods will the compiler generate for you>?
Can we override constructors?
what is the purpose of the runtime class?