Java Programming - Objects and Collections - Discussion

2. 

Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?

[A]. java.lang.String
[B]. java.lang.Double
[C]. java.lang.StringBuffer
[D]. java.lang.Character

Answer: Option C

Explanation:

java.lang.StringBuffer is the only class in the list that uses the default methods provided by class Object.


Vidya said: (Jul 18, 2010)  
can u give me the clear discription?

Firat said: (Feb 7, 2012)  
Other classes are wrapper classes and override equals() and hashCode() methods. The StringBuffer does not override these methods.

SArfaraz Ahamad said: (May 14, 2012)  
But String class is not Wrapper class.

Pankaj Kalra said: (Jul 3, 2012)  
Why string buffer or string builder not override these 2 mehtods ?

Ghost said: (Nov 26, 2012)  
@Sarfaraz Ahamad.

All wrapper classes and String class overrides the equals() and hashCode().

@Pankaj Kalra.

string buffer or string builder are not classes .
The classes are StringBuffer and StringBuilder. Please follow java naming conventions.

Manoj said: (Sep 26, 2013)  
Please could you give full description?

Why StringBuffer class does not support direct overriding these 2 methods?

Monika said: (Jan 13, 2014)  
I think StringBuffer just uses these two methods as they are provided from Object class. The other three classes override them.

G Sunil Kumar said: (Aug 7, 2014)  
Why the special behavior for StringBuffer class only?

Narasimha said: (Dec 3, 2014)  
I am confusing why StringBuffer and StringBuilder are not able to overriding these two methods equals and hashcode() methods. Please could you clarify the same. Thank you.

Asdf said: (Mar 11, 2015)  
I think StringBuffer and StringBuilder are class use to make string immutable.

Vinod said: (Mar 25, 2015)  
Because StringBuffer is mutable, and its primary use is for constructing strings. If you want to compare content, call StringBuffer#toString() and compare the returned value.

It is not generally useful to override hashCode() for mutable objects, since modifying such an object that is used as a key in a Hash Map could cause the stored value to be "lost".

Anoop Kumar said: (May 1, 2017)  
Please tell me, what you mean by overriding?

Gayudhaya said: (Jul 7, 2017)  
I didn't understand it can you give a clear explanation?

Ravindra Babu said: (Dec 5, 2017)  
Java.lang.StringBuffer is the only class that uses default methods provided by class object.

Santhosh said: (Jan 20, 2018)  
I didn't understand it. Can you give a clear explanation?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.