Equals method

The equals method applied to a String, however that String was created, performs a character by character comparison.

I was reading above concept from link

http://www.jchq.net/certkey/0502certkey.htm

I was not clear on what it means by 'performs a character by character comparison.'

Any ideas, resources,sample code,links,  highly appreciated. thanks in advance.
LVL 7
gudii9Asked:
Who is Participating?
 
for_yanCommented:
Read these excerpts for Java API for hashcode()
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#hashCode%28%29
and for equals()
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#equals%28java.lang.Object%29


You'll first see my point that hashcode() is a function of just one object, so by itself it does not compare  two objects, like equals(Object o), which in general situation uses two objects - the object on which this method is running and the object which is supplied by the argument

Another distiction that hashcode() returns integer
and equals() returns boolean

Otherwise there are of course some things to think about in these
descriptions

0
 
for_yanCommented:
"Chararcter by character compatison " - it is simple

let's say we compare

strings

"colonel"

and

"column"

we comaper first character in both - they are "c" - indetical
second carcahcter - "o" - identical
third ccharcater "l" - identical
fourth character  in "colonel" - it would be "o" , in "column" - it would be "u"
"o" is not the same as "u" - so these two strings are different

if we would have come to the end of the string and all charchters
are matching - those strings would be equal,
that is what method   equals(String) performs




 
0
 
gudii9Author Commented:
does it different from absolute comparison. what is shallow comparison. What are different kinds of comparisons available. Please advise
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
for_yanCommented:


This is about shallow and deep equals :
http://lasitheranda.wordpress.com/2009/08/26/shallow-equals-and-deep-equals-using-java-equals-method/

In general I would not go too much into that detail about deep and shallow.

About equals it is good to know how override this method
and that string should be compared only using this method

comparison of strings charcater by character as far as I understand
is as deep as possible - there is no deeper level in the String
which we do not check.


0
 
gudii9Author Commented:
hashcode() just compares the reference variables of the object reference right unlike each and every character. please advise
0
 
for_yanCommented:
hashcode() does not compare any objects.
hashcode() returns number which is unique for the object. But as opposed to equals it does not return boolean and it does not mention second object.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.