wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals() method and == operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals() method and == operator" Watch "equals() method and == operator" New topic
Author

equals() method and == operator

Pradeep Balasubramanian
Ranch Hand

Joined: Jun 16, 2008
Posts: 75
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.

???


Regards,<br />Pradeep Balasubramanian<br /> <br />You have to grow from the inside out. None can teach you, none can make you spiritual. There is no other teacher but your own soul. <br /> <br />- Swami Vivekananda
vara raju
Greenhorn

Joined: Sep 06, 2008
Posts: 13
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.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Maggie Zhou
Greenhorn

Joined: Sep 09, 2008
Posts: 26
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 ]

Preparing SCJP 6, try to get high score, and looking for a job...
Sambit Banerjee
Greenhorn

Joined: Jul 29, 2008
Posts: 18
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 ]
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

@ Sambit

Yes it will be false ..


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Pradeep Balasubramanian
Ranch Hand

Joined: Jun 16, 2008
Posts: 75
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.

ankana mukherjee
Greenhorn

Joined: Aug 22, 2008
Posts: 6
why fg==fg1 false?please reply.
Pradeep Balasubramanian
Ranch Hand

Joined: Jun 16, 2008
Posts: 75
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
thea melianta
Greenhorn

Joined: Aug 31, 2008
Posts: 9
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

Joined: Jun 16, 2008
Posts: 75
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

Joined: Aug 31, 2008
Posts: 9
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..

Sudarshan Chakrabarty
Ranch Hand

Joined: Apr 10, 2008
Posts: 38
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

Joined: Apr 10, 2008
Posts: 38
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.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
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

Joined: Apr 10, 2008
Posts: 38
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

Joined: Sep 13, 2008
Posts: 178
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

Joined: Aug 31, 2008
Posts: 9
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals() method and == operator