• YES StringBuffer and StringBuilder classes not overriding equals()method and haschcode() method.
  • Before discussing about why these classes are not overriding equas() and hashcde() methods lets see the usage of overriding equals and hashcode() methods.
  • String class is overriding these equals() and hashcode() methods.
  • When we want to compare two strings we use equals method.
  • basically equals() method is defined in Object class and it will compares the references of two objects if those two objects reference is same then its returns true.
  • And if equals() methods returns true on comparing two objects then their hashcode() must be same this is the contract between equals() and hashcode().
  • Lets see a java program which compares two strings.


  1. package com.instanceofjava;
  2.  
  3. class StringEqualsDemo{
  4.  
  5. public static void main(String [] args){ 
  6.  
  7. String fstr= new String("Javatutorials");
  8. String sstr= new String("Javatutorials"); 
  9.  
  10. System.out.println(fstr.equals(sstr));
  11. System.out.println(fstr==sstr);
  12.  
  13. System.out.println(fstr.hashCode());
  14. System.out.println(sstr.hashCode());
  15. }
  16. }
Output:
  1. true
  2. false
  3. 1921912019
  4. 1921912019
  • In the above program we compared two string using equals() method and it returns true.and comparing using == operator returns false.
  • Basically equal() will also return false on comparing those two strings because default functionality of equal() method is to compare references and two strings are created using new operator so both references are different.
  • But String class overriding equals() method and in that equals method it comparing content of the strings and returning true if both are having same content false if not.
  • Lets see what happen if we compare two stringBuffer objects using equals() method.


  1. package com.instanceofjava;
  2.  
  3. class StringBufferEqualsDemo{
  4.  
  5. public static void main(String [] args){ 
  6.  
  7. StringBuffer fstr= new StringBuffer("Javatutorials");
  8. StringBuffer sstr= new StringBuffer("Javatutorials"); 
  9.  
  10. System.out.println(fstr.equals(sstr));
  11. System.out.println(fstr==sstr);
  12.  
  13. System.out.println(fstr.hashCode());
  14. System.out.println(sstr.hashCode());
  15. }
  16. }
Output:
  1. false
  2. false
  3. 1704856573
  4. 705927765
     
  • If you observe above java program when we are comparing two stringBuffer objects equal() method returning false even content is same. Because StringBuffer class not overriding equals() and hashcode() methods.
  • StringBuilder is also not overriding equals() method? lets see a program and clarify. 


  1. package com.instanceofjava;
  2.  
  3. class StringBuilderEqualsDemo{
  4.  
  5. public static void main(String [] args){ 
  6.  
  7. StringBuilder fstr= new StringBuilder("Javatutorials");
  8. StringBuilder sstr= new StringBuilder("Javatutorials"); 
  9.  
  10. System.out.println(fstr.equals(sstr));
  11. System.out.println(fstr==sstr);
  12.  
  13. System.out.println(fstr.hashCode());
  14. System.out.println(sstr.hashCode());
  15. }
  16. }
Output:
  1. false
  2. false
  3. 1704856573
  4. 705927765
  • So now its cleared that StringBuffer and StringBuilder classes not overriding equals() and hashCode() methods.
  • But Why?
  • Why StringBuffer and StringBuilder classes not overriding equals() method and hashcode() method where as String class is overriding these two methods.
  • Basically Strings are Immutable means Whenever we try to change the value of string result will be new string. So string content wont change.
  • StringBuffer main use is mutable means when we append a string to it it will add to existing object.
  • When the content changes the hashcode will changes. 
  • Lets see a program on adding elements to hashmap.


  1. package com.instanceofjava;
  2.  
  3. class StringDemo{
  4.  
  5. public static void main(String [] args){ 
  6.  
  7. String fstr= new String("Javatutorials");
  8. String sstr= new String("Javatutorials"); 
  9.  
  10.  Hashtable ht=new Hashtable();
  11.  
  12.         ht.put(fstr, "abc");
  13.         ht.put(sstr, "xyz");
  14.         
  15.         System.out.println(ht);
  16. }
  17. }
Output:
  1. {Javatutorials=xyz}


  • In the above java program we tried to add two strings objects as keys to the hashtable.
  • Hashtable put method internally calles equals() method and if its true it wont add.
  1. package com.instanceofjava;
  2.  
  3. class StringBufferHashtableDemo{
  4.  
  5. public static void main(String [] args){ 
  6.  
  7. StringBuffer fstr= new StringBuffer("Javatutorials");
  8. StringBuffer sstr= new StringBuffer("Javatutorials"); 
  9.  
  10.  Hashtable ht=new Hashtable();
  11.  
  12.         ht.put(fstr, "abc");
  13.         ht.put(sstr, "xyz");
  14.         
  15.         System.out.println(ht);
  16. }
  17. }
Output:
  1. {Javatutorials=xyz, Javatutorials=abc}



  1. package com.instanceofjava;
  2.  
  3. class StringBufferHashtableDemo{
  4.  
  5. public static void main(String [] args){ 
  6.  
  7. StringBuilder fstr= new StringBuilder("Javatutorials");
  8. StringBuilder sstr= new StringBuilder("Javatutorials"); 
  9.  
  10.  Hashtable ht=new Hashtable();
  11.  
  12.         ht.put(fstr, "abc");
  13.         ht.put(sstr, "xyz");
  14.         
  15.         System.out.println(ht);
  16.  
  17. }
  18. }
Output:
  1. {Javatutorials=xyz, Javatutorials=abc}


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

3 comments for Why StringBuffer Class not overriding equals and hashCode methods

Select Menu