File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String class and StringBuffer class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String class and StringBuffer class" Watch "String class and StringBuffer class" New topic
Author

String class and StringBuffer class

JOSEPH BIH
Ranch Hand

Joined: Jul 08, 2001
Posts: 44
Can anyone answer the following question? Thanks!
There is no problem comparing string objects, but why comparing stringbuffer objects are not allowed or it returns false.
StringBuffer sb1 = new StringBuffer("Aha");
StringBuffer sb2 = new StringBuffer("Aha");
String str = "Aha";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(str));
System.out.println("Pear".substring(3));
Answer: false false false r
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Here's the problem - StringBuffer doesn't have an equals() method , so it get's the equals() from the Object class .
From the API
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
Hope that helps
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
To add more to Asish's Reply
StringBuffer class does not override equals method which it had inherited from object class.
The object class equals() does shallow comparison; that is checking the references whether thay are same.. which by all means same as == method.(Of course there are differences in the efficiency, speed of these two methods doing the same thing.. but as for as SCJP is concerned they do the same shallow comparison)
For the equals method to do deep comparison such as checking the contents rathser than object references they need to be overridden.
String classes & Wrapper classes override the equals() todo deep comparison.
HTH
Any comments.....Welcome.
tvs sundaram
JOSEPH BIH
Ranch Hand

Joined: Jul 08, 2001
Posts: 44
Thanks, guys!
/bStringBuffer class does not override equals method which it had inherited from object class.b/
So, Sundaram. Only the Wrapper and String class have their own eauals() methods that can overwrite the Object class equals() methods?
Joseph
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
To my knowledge; Yes ; Only these two. Ranch gurus can add comments pls.
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
I'll have to agree to that . BTW what is a shallow comparision & a deep comparison , my book sez nothin 'bout these .
Thanx
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
Hai Ashish,
Shallow comparison:
Finding out whether two object references share the same memory address.
Deep comparison:
Finding out the whether the contents of the two object references are same.
(Of course there are other conditions like that there will be check whether the operands are really objects, whether they belong to the same type)
HTH
I think RHE uses deep, shallow phrases
tvs
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Thanx Sundaram
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String class and StringBuffer class