ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

Why StringBuffer and StringBuilder has not overridden equals() and hashCode() in java ?

user-image
تم إضافة السؤال من قبل Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution
تاريخ النشر: 2014/12/27
Mohamed Alesh
من قبل Mohamed Alesh , Independent Contractor / Consultant , Upwork (Engineering Team - APIs)

Design wise, I think they shouldn't override equals nor hashcode methods since they are just builder objects. The state for these objects is just the character array from the AbstractStringBuidler. Logically, you wouldn't need to check the equality of two builder objects (someone shouldn't really care about the state of the builder) rather you need to check the equality of what is being built, which is the target String object in this case.

Allahuddin Syed
من قبل Allahuddin Syed , BIG BAZAAR (FUTURE GROUP)

Because String Buffer is mutable, and its primary use is for constructing strings. If you want to compare content, call String Buffer#to String() and compare the returned value.

It is not generally useful to override hash Code() 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."

Mohammed Mozakir
من قبل Mohammed Mozakir , Software Developer , Traitsys Technology Pvt. Ltd.

Because it is a mutable 

pardeep kumar
من قبل pardeep kumar , Senior R & D Engineer , Mavenir Systems

Simplest reason is that because they are mutable in Java.  Understand the concept of immutability in String, you get to know automatically why these methods are not overridden. 

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟