• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals() method and == operator

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference in using the equals() method and == operator in java.

Why is there a need for equals() method when there is a == operator

or

Why is there a need for == operator when there is a equasl() method.

???
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
welcome to javaranch.

String a = new String("abc");
String b = new String("abc");


a.equals(b)
--> here equals() checks whether these two objects are contains the
same value or not.
if(a == b)
--> here == will check whether these two objects are sharing the same
memory location or not.

for Example:
String c = "abc";
String d = "abc";
Now c and d are sharing the same memory location.
Hence, if we say c == d will be true.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pradeep 31 posts, Raju 8 posts, Raju Welcomes Pradeep???

You can simple understand it as == checks that 2 references refer to the same block of memory and equals checks whether two objects are meaningfully equal. Two strings are meaningfully equal if they have the same text. Similarly two humans will be equal if they have the same DNA or fingerprint(just an example)...
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my own notes:

==:
for primitive: check the velue

for String:
1.if see any new(means String s = new String("xxx"); -->check if they pointing to same object
2.if see only ""(means String s = "xxx"; ) -->check the string content

for Wrapper:
1. same as String, see any new --> check if they pointing to same object
2, if there is not, then tricky things coming:
2.1, if it is byte, short, int and the value is <=127, -->check the value
2.2, otherwise check the if they pointing to same object

equals():
for general Object:
1.must override it to compare the key attribute(s) of object;
2,if collection is Hashxxx, you need override hashCode() too, otherwise equals won't effect

for String and wrappers:
they have well overridden equals() and hashCode()

============================
If I have something missing, or uncorrect, please please let me know, thanks

[ September 10, 2008: Message edited by: Doraemon Zhou ]
[ September 10, 2008: Message edited by: Doraemon Zhou ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a query related to Vara Raju's example.
for the first example (a==b) will be false just because they will refer to different memory location in the non-pool memory?
Am i correct?
[ September 10, 2008: Message edited by: Sambit Banerjee ]
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Sambit

Yes it will be false ..
 
Pradeep Balasubramanian
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You can simple understand it as == checks that 2 references refer to the same block of memory and equals checks whether two objects are meaningfully equal. Two strings are meaningfully equal if they have the same text. Similarly two humans will be equal if they have the same DNA or fingerprint(just an example)...



It is a good explanation by Ankit!
Thank you , Ankit!

I just tried the following code
...
..
.



and got the following output


a : ab
b : ab
a.equals(b) : true
a==b : true

cd : Jean
e : Jean
cd.equals(e) : true
cd==e : true

fg : forgery
fg1 : forgery

fg.equals(fg1) : true
fg==fg1 : false

h : abcdefg
i : abcdefg
h.equals(i) : true
h==i : false

j : abc
k : abc
j.equals(k) : true
j==k : false


Hence,
I came to a conclusion

== operator looks for the references
equals() method looks for the values.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why fg==fg1 false?please reply.
 
Pradeep Balasubramanian
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my code snippet,

String f = "for";
String g = "gery";
String fg = f + g;
String fg1 = "forgery";

Condition to be tested :
fg == fg1
return type : boolean

fg == fg1
Here == looks for whether the references are the same.
The string 'fg' is obtained from two strings which refer to two different objects
The string 'fg1' refers to a single object "forgery".

fg will result in "forgery" as shown in the output.
fg1 will also result in "forgery"

the objects are physically, the same .
Which means fg.equals(fg1) is a true statement

fg == fg1 ?
Here fg and fg1 have different references.
Hence
for fg==fg1 the return type is false
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you give more explanation about fg==fg1 is false?
fg also produces String with value "forgery", doesn't it?
why fg and fg1 not referring the same object String which has value "forgery"?
thanks
 
Pradeep Balasubramanian
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Better Explanation,
Refer as follows :

Book : SCJP Sun Certified Programmer for Java 5 Study Guide
Author : Kathy Sierra and Bert Bates.
Chapter Six : Strings, I/O, Formatting, and Parsing
Figure 6-1 and 6-2




Hope, this helps?!

bye...
[ September 17, 2008: Message edited by: Pradeep Balasubramanian ]
 
thea melianta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am sorry
i already read that chapter..
but still don't get it why fg==fg1 is false?

maybe you can explain it with picture like 6-1 specially when "fg=f+g", which makes it different with fg1..

thanks..

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thea,

There are a couple of concepts you need to understand here :
i) String objects are immutable which means that a String object once created cannot be modified. Trying to modify an existing String actually results in creation of a new String.

ii) If String objects having the same data are created using a constant expression, a string literal, a reference to an existing string, or by explicitly using the intern() method, their references will be the same.

iii)If String objects having the same data are created explicitly with the new operator or " their values are computed at runtime", their references will be different.

Now, we have the following code:
String f = "for";
String g = "gery";
String fg = f + g; //which will result in "forgery" at run time
String fg1 = "forgery"; // String literal
String fg2= "for" + "gery"; // "forgery" created using String literal

Condition to be tested :
fg == fg1 will return false
because of point iii) mentioned above. The value of fg is calculated at run time.

fg== fg2 will return true
Explanation:
Here, both the value of fg2 and fg are calculated using String literal. So, according to point ii) they are equal.

Hope this clears your doubt.

For more details http://www.janeg.ca/scjp/lang/strLiteral.html
 
Sudarshan Chakrabarty
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Small correction to the above.

fg1 == fg2 will return true and not fg == fg2 ( Sorry for that )

Explanation:
Here, both the value of fg2 and fg1 are calculated using String literals.
String fg2= "for" + "gery"; ( "for" and "gery" both being String literals)
String fg1 = "forgery"; // String literal
So, as we know if String objects having the same data are created using a a string literal their references will be the same.
Hence, fg1== fg2.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For fg1 == fg2, "false" will be the answer as those two are two different references.
But, fg1.equals (fg2) returns "true" as the contents are the same (meaningfully equivalent concept)
 
Sudarshan Chakrabarty
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rekha,

You can run this sample program and check the output

public class StringTest {

public static void main(String[] args) {
String f = "for";
String g = "gery";
String fg = f + g;
String fg1 ="forgery";
fg="for" + "gery";

if(fg == fg1){
System.out.println(" fg==fg1");

}
else {
System.out.println(" fg!=fg1");

}

}
}
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oooops.. The "false" which I got earlier was due to the following erroneous print statement, without parentheses:
System.out.println (" Result of == test" + fg1 == fg2);

The print statement should have been
System.out.println (" Result of == test" + (fg1 == fg2));
to get "true" as the result (fg1 and fg2 are really equal)

Watch out :-)

Thanks Sudarshan...
 
thea melianta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Sudarshan..

i think i getting more understand with your concepts especially number 3(iii)..
fg=f+g which will result in "forgery" at run time..
now i know why fg == fg1 is false..

thanks
 
The knights of nee want a shrubbery. And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic