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?

Answers were Sorted based on User's Feedback



If I will write String s=new String("XYZ"); String s1=new String("XYZ"); if(..

Answer / ashish singh

String and other wrapper classes override equals() method of object class. SO when we compare it gives the appropriate result. while the employee class did not do it.

Is This Answer Correct ?    10 Yes 0 No

If I will write String s=new String("XYZ"); String s1=new String("XYZ"); if(..

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

If I will write String s=new String("XYZ"); String s1=new String("XYZ"); if(..

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

If I will write String s=new String("XYZ"); String s1=new String("XYZ"); if(..

Answer / k.packiya lakshmi

In String we can write a string in double quotes and equals is
one method of string but when we creating class it doesn't
know the method of equals.

Is This Answer Correct ?    3 Yes 3 No

If I will write String s=new String("XYZ"); String s1=new String("XYZ"); if(..

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

More Core Java Interview Questions

What is a private class in java?

0 Answers  


What is CardLayout?

1 Answers   Infosys,


What is OOP Language?

0 Answers   Atos Origin,


Which is easier netbeans or eclipse?

0 Answers  


Explain the difference between collection api and stream api in java8?

0 Answers  


What is length in java?

0 Answers  


Can we call the run() method instead of start()?

0 Answers  


What type of value does sizeof return?

0 Answers  


What do you mean by jjs in java8?

0 Answers  


What are the uses of final, finally and finalize in java?

2 Answers   Agiline, FINO PayTech, HCL,


How to do a true java ping from windows?

0 Answers  


Can It is possible to synchronize the constructor of a Java Class?

0 Answers  


Categories