• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EQUALS method

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc i got it
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code Tags come in handy here.
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton padma and marc, it's all clear now..
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

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

Regards
Padma
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic