The Artima Developer Community
Sponsored Link

Java Buzz Forum
Why StringBuffer Class not overriding equals and hashCode methods

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Why StringBuffer Class not overriding equals and hashCode methods Posted: Aug 15, 2015 7:33 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Why StringBuffer Class not overriding equals and hashCode methods
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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}


Read: Why StringBuffer Class not overriding equals and hashCode methods

Topic: Java Tutorial : Java Inheritance - Playlist Previous Topic   Next Topic Topic: Up your coding game with these 7 habits of great programmers

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use