Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Extending Ashish Singh's answer, If you want employee to return true for the use case you have mentioned, override your equals and hashcode method and check only for the String object that you have in employee class in your overridden equals method. It'll return true.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between abstraction and encapsulation?

1017


how to handle exceptions in ejb?

2273


Which one will take more memory: an int or integer?

1184


Can we define a package statement after the import statement in java?

1030


Which is the best approach for creating thread ?

1154


What is an empirical question?

922


Difference between stack and queue?

1035


What kind of variables can a class consist?

1011


What does math floor () do?

925


What is the difference between error and an exception?

1083


How will you add panel to a frame?

1071


What is the parse method in java?

1065


Why singleton pattern is better than creating singleton class with static instance?

969


What is meant by stack and queue?

1019


What are heap memory and stack memory and what are memory tables.

940