AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-string-handling: Why StringBuffer Class not overriding equals and hashCode methods?

Please explain why StringBuffer Class not overriding equals and hashCode methods.

corejava x 347
string-handling x 34
Posted On : 2016-05-16 22:30:10.0
profile Saksham Kumar - anyforum.in Saksham Kumar
72933914652
up-rate
5
down-rate

Answers


StringBuffer and StringBuilder classes do not overriding equals()method and haschcode() method. String class overrides 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 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.

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());
}
}


Output:
---------------------------------
true
false
1223121231
1223121231


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.

Basically Strings are Immutable means Whenever we try to change the value of string result will be new string. So string content won´t change.

StringBuffer´s 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 change.

Lets see a program on adding elements to hashmap.

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);
}
}

Output:
---------------------------------
{Javatutorials=xyz}

In the above java program we tried to add two strings objects as keys to the hashtable. Hashtable put method internally calls equals() method and if its true it won´t add.

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);
}
}


Output:
{Javatutorials=xyz, Javatutorials=abc}

Posted On : 2016-05-16 22:53:42
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185519807
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in