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 / prachi
Equals() method of Qbject class checks whether reference id's are same or not.if overridden then it will check for the value contained in those reference id's.String class has overridden equals() so it checks for the value whereas Employee class hasn't so Object class' method runs and reference id's are compared.
| Is This Answer Correct ? | 2 Yes | 0 No |
Post New Answer View All Answers
Explain about java sdk?
Can we write multiple catch blocks under single try block?
What is java argument list?
What exactly is methodology?
Write an algorithm program in java for the following question.. In a VLSI design techniques,they used rectangles to design circuits. EVery rectangle is to be placed according to x,y coordinates. Check whether or not two rectangles overlap each other. Here overlapping of rectangles is acceptable, if 1) one rectangle intersect with other. 2) one rectangle fully covers other. The time of algorithm should not exceed o(n logn).
How static variable work in java?
State the merge-sort principle and its time complexity.
Explain the use of javap tool.
In the below example, how many string objects are created?
Can interface be private in java?
What are the actions that can occur when a thread enters blocked state?
How to display all the prime numbers between 1 and n (n is the number, get the input from user)
What are different data structures in java?
What is static in java?
What does nullpointerexception mean?