Custom object Vs String Object

As per below paper
http://javapapers.com/core-java/hashcode-and-equals-methods-override/

To consolidate the concept of equals() and hashCode(), we are saying for any custom objects like Tiger etc we have to override equals() method since we do not know if want to define equality based on color or stripes etc. Since we override equals() for custom objects we should definitely override hashCode() method also to avoid the corruption.

For String objects we should always use default parent class Object .equals() (as there are nothing like color, stripes etc) to compare strings not ==(which is mostly used for primitives)

For String objects hashCode() need not be implemented since .equals() we are not overriding.

I was reading more here
http://tutorials.jenkov.com/java-collections/hashcode-equals.html
But String equals() and hashCode() is still not as clear as Custom Objects equals() hashCode()
please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
Everything you say at the top sounds correct. It looks like you're got a good understanding of when to write equals() and hashCode() and when not to.

But String equals() and hashCode() is still not as clear as Custom Objects equals() hashCode()
please advise

I'm not sure what you're confused about here.  Can you point to the specific piece of code or behavior that is confusing you in this article?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
when we say if two string objects(A,B) are equal?

when the
A.equals(B) true  Is my understanding is correct?

For String objects hashCode() need not be implemented since .equals() we are not overriding.

Is my understanding about above point is correct?
please advise
0
ozoCommented:
If the default behavior suits your purposes, you do not need to override.
0
dpearsonCommented:
when we say if two string objects(A,B) are equal?
when the
A.equals(B) true  Is my understanding is correct?

Yes that's right - A and B are equal when the equals() method returns true.

For String objects hashCode() need not be implemented since .equals() we are not overriding.
Is my understanding about above point is correct?

Yes that's right.  If you are using String objects you don't need to implement equals() or hashCode().  (And yes if you did decide to implement either equals() or hashCode() you always need to implement both of them).

Doug
0
krakatoaCommented:
You can't override equals for a String anyway. How does anyone suggest you do that?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

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.