where is .equals() method and how is it different from ==
operation <giving some confusing implementation> Is hashing
related to these?
Answer Posted / 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 |
Post New Answer View All Answers
What is the difference between logical data independence and physical data independence?
What is polymorphism java example?
What is the difference between a scrollbar and a scrollpane?
Is an object null?
Why does java doesnt suuport unsigned values?
What is the use of conditional statement?
What does the exclamation mark mean in java?
We are seeing so many videos/audios as many web sited. But question is these videos or audios are stored in Databases ( Oracle, Mysql, Sybase,... ) or stored any file directory from there they will give the link for that? Pls explain and give sample code to achieve this one? Thanks, Seenu.
What happens when a thread cannot acquire a lock on an object in java programming?
Does printwriter create a file?
What is difference between ++ I and I ++ in java?
Explain different types of wrapper classes in java?
What are the features of junit?
Difference between collection, collection and collections in java?
How many classes can any class inherit java?