aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes EQUALS method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "EQUALS method" Watch "EQUALS method" New topic
Author

EQUALS method

chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Hello friends

just wanted to make sure that, you can't override equals() for StringBuffer class ? but still Object method can be used with StringBuffer Class.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Overriding means: you extend a class, and in your subclass you add a method that has the same name and arguments as a method in the superclass.

Class StringBuffer is final, which means you cannot extend it. So, you cannot create a subclass of StringBuffer and override StringBuffer's equals() method.

But I have a feeling that this is not what you wanted to ask. So, can you explain exactly what you mean?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

StringBuffer inherits the implementation of equals defined in Object. It does not override the method. As Jesper pointed out, StringBuffer is final, so you cannot extend the class and override the method with your own implementation.

For the type of equals behavior you're probably looking for, you can obtain a String by calling toString and use String's implementation of equals...

(buffer1.toString()).equals(buffer2.toString())

Note: The same goes for StringBuilder.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Thanks marc i got it
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Hi Jesper,

If we override implement the EQUALS method for StringBuffer class, we cannot invoke our implemented method, instead Object's equal method will be called and result will be "FALSE" even though we have same object of StringBuffer.

may be this code may clear it

public class Buffer {

public boolean equals(Object o){
if(o instanceof StringBuffer )
return true;

return false ;
}

public static void main(String []args){

StringBuffer sb = new StringBuffer("TRUE");
StringBuffer s = new StringBuffer("TRUE");
System.out.println(s.equals(sb));
}
}

- when you run this, you are supposed to get true, but object's class equals method get run though we have overriden equals.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by chintan ramavat:
...may be this code may clear it...

In that code, you are creating an entirely new class called Buffer (which is not a StringBuffer), and overridding the equals method for instances of that class.

Your equals method will return true if you create an instance of Buffer and then use equals to compare it to an instance of StringBuffer...
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
oops it was a mistake, thanks marc for pointing it out. yeah you are right, but i come across one of the exam question, and answer was like StringBuffer does not override equals method meaning equals on string buffer object returns false.
Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi

I had the similar concern few days ago but it is clarified now. I hope this example will help.

public class StringTest {
public static void main(String args[]) {
StringBuffer sb1 = new StringBuffer("ABC");
StringBuffer sb2 = new StringBuffer("ABC");
StringBuilder sbld = new StringBuilder("ABC");
String s = new String("ABC");
if(sb1.equals(sb2))
System.out.println("Equals");
else System.out.println("No they are not");
/* Word around to compare two StringBuffer * or StringBuilder objects */
if(sb1.toString().equals(sb2.toString()))
System.out.println("Equals");
else
System.out.println("Not Equals");
if(s.equals(sb1))
System.out.println("YES");
else
System.out.println("No they are not"); if(s.equals(sbld.toString()))
System.out.println("Now they are");
else
System.out.println("We are not reaching here");
}
}

OUTPUT:
No they are not
Equals
No they are not
Now they are

NOTE: The key is StringBuffer and StringBuilder do not override equals methods, so the objects of StringBuilder and StringBuffer use equals method of object class, while this is not true of String class.

Regards
Padma
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Code Tags come in handy here.
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Thanks a ton padma and marc, it's all clear now..
Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi Chintan,

Sorry I forgot the mention the previous thread. See the following thread where it all started and it was Marc who clarified it.

http://www.coderanch.com/t/263409/java-programmer-SCJP/certification/Explanation-required-equals-method-String

Regards
Padma
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EQUALS method