- 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.
- package com.instanceofjava;
- class StringEqualsDemo{
- public static void main(String [] args){
- String fstr= new String("Javatutorials");
- String sstr= new String("Javatutorials");
- System.out.println(fstr.equals(sstr));
- System.out.println(fstr==sstr);
- System.out.println(fstr.hashCode());
- System.out.println(sstr.hashCode());
- }
- }
- true
- false
- 1921912019
- 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.
- package com.instanceofjava;
- class StringBufferEqualsDemo{
- public static void main(String [] args){
- StringBuffer fstr= new StringBuffer("Javatutorials");
- StringBuffer sstr= new StringBuffer("Javatutorials");
- System.out.println(fstr.equals(sstr));
- System.out.println(fstr==sstr);
- System.out.println(fstr.hashCode());
- System.out.println(sstr.hashCode());
- }
- }
- false
- false
- 1704856573
- 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.
- package com.instanceofjava;
- class StringBuilderEqualsDemo{
- public static void main(String [] args){
- StringBuilder fstr= new StringBuilder("Javatutorials");
- StringBuilder sstr= new StringBuilder("Javatutorials");
- System.out.println(fstr.equals(sstr));
- System.out.println(fstr==sstr);
- System.out.println(fstr.hashCode());
- System.out.println(sstr.hashCode());
- }
- }
- false
- false
- 1704856573
- 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.
- package com.instanceofjava;
- class StringDemo{
- public static void main(String [] args){
- String fstr= new String("Javatutorials");
- String sstr= new String("Javatutorials");
- Hashtable ht=new Hashtable();
- ht.put(fstr, "abc");
- ht.put(sstr, "xyz");
- System.out.println(ht);
- }
- }
- {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.
- package com.instanceofjava;
- class StringBufferHashtableDemo{
- public static void main(String [] args){
- StringBuffer fstr= new StringBuffer("Javatutorials");
- StringBuffer sstr= new StringBuffer("Javatutorials");
- Hashtable ht=new Hashtable();
- ht.put(fstr, "abc");
- ht.put(sstr, "xyz");
- System.out.println(ht);
- }
- }
- {Javatutorials=xyz, Javatutorials=abc}
- package com.instanceofjava;
- class StringBufferHashtableDemo{
- public static void main(String [] args){
- StringBuilder fstr= new StringBuilder("Javatutorials");
- StringBuilder sstr= new StringBuilder("Javatutorials");
- Hashtable ht=new Hashtable();
- ht.put(fstr, "abc");
- ht.put(sstr, "xyz");
- System.out.println(ht);
- }
- }
- {Javatutorials=xyz, Javatutorials=abc}
nice one very easy to understand
ReplyDeleteGREAT MAN ...KEEP HELPING
ReplyDeleteamazing expl
ReplyDelete