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...


where is .equals() method and how is it different from ==
operation <giving some confusing implementation> Is hashing
related to these?

Answers were Sorted based on User's Feedback



where is .equals() method and how is it different from == operation <giving some confusing impl..

Answer / tanuj

== operator return true if both reference variable pointing
to same object.
equals() method also use == as default, but String and
Wrapper classes override (you can also override) it but
meaningfull comparison , as in two same String case it
returns true for equals()method.

for knowing it clearly make small programs by using both.
there are so many exception in wrapper classes.

Is This Answer Correct ?    7 Yes 0 No

where is .equals() method and how is it different from == operation <giving some confusing impl..

Answer / namita

equals() method is used for comparison. it copares the
content whereas == is used to compare the references.

if the hashcode value is same for two objects equals()
method will return true. but in case of == it is not the
case.

Try following code

String str="Java";
String str1="Ja";
String str2="va";
String str3=str1+str2;
String str4 = new String("Java");
String str5 = str4;
String str6 = new String("Programming");
String str7 = new String(str6);
System.out.println(str3);
System.out.println("str "+str.hashCode());

System.out.println("str3 :- "+str3.hashCode());
System.out.println("str4 :- "+str4.hashCode());
System.out.println("str5 :- "+str5.hashCode());
System.out.println("str6 :- "+str4.hashCode());
System.out.println("str7 :- "+str5.hashCode());


if(str==str3)
System.out.println("str and str3 are equal(==)");
else
System.out.println("str and str3 are not equals
(==)");
if(str.equals(str3))
System.out.println("str and str3 are equals(.equals
())");
else
System.out.println("str and str3 are not equals
(.equals())");


if(str4==str5)
System.out.println("str4 and str5 are equal(==)");
else
System.out.println("str4 and str5 are not equals
(==)");
if(str.equals(str3))
System.out.println("equals");
else
System.out.println("not equals");


if(str6==str7)
System.out.println("str6 and str7 are equal(==)");
else
System.out.println("str6 and str7 are not equals
(==)");
if(str.equals(str3))
System.out.println("equals");
else
System.out.println("not equals");

Is This Answer Correct ?    6 Yes 0 No

where is .equals() method and how is it different from == operation <giving some confusing impl..

Answer / esha prasad

.equlas() method is in Object class which compares two
objects and returns true if both are same.

== compares whether two references pointing to same memory
location.

Is This Answer Correct ?    2 Yes 2 No

where is .equals() method and how is it different from == operation <giving some confusing impl..

Answer / pavan kumar pathivada

The code by Namita works successfully but you need to make
certain changes to understand the concept.for every .equals
comparison in the code only two strings are considered
everytime i.e str and str3. Note that the hash code
functions also need to changed for str6 and str7.
anywys...great work namita...helped me a lot...thnks...

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

Explain Connection Pooling?

3 Answers  


Can you extend main method in java?

0 Answers  


Can we make the abstract methods static in java?

0 Answers  


What is Mutex (Mutual Exclusion Object) ?

0 Answers   Ciena,


Can we make a constructor final?

0 Answers  


What is string immutability?

0 Answers  


Why super is first line in java?

0 Answers  


What is the history of java?

0 Answers  


Describe inheritance as applied to java?

4 Answers  


What is java basic concept?

0 Answers  


What is java reflection?

0 Answers  


What is JDK? Mention the variants of JDK?

1 Answers  


Categories