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 Buffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Buffer" Watch "String Buffer" New topic
Author

String Buffer

Rajiv Ranjan
Ranch Hand

Joined: Sep 28, 2000
Posts: 61
Hey Guys,
Is there any particular reason why method equals is not overridden in StringBuffer class?
If you test this piece of code :
public class TestClass
{
public static void main(String Args[])
{
StringBuffer sb1 = new StringBuffer("String");
StringBuffer sb2 = new StringBuffer("String");
if(sb1.equals(sb2))
{
//lots of code
}
}
}

The author of the question said "Line marked with lots of code is never reached" and he is correct (as I modified and tested the code).
ming fan
Greenhorn

Joined: Nov 15, 2000
Posts: 21
According to RHE JAVA 2 Study Guide, Chapter 8, java.lang and java.util Packages, StringBuffer inherits from the Object equals method, so it doesn't give you what you expected.
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
Hi,
The equals() method belongs to java.lang.Object and hence any class can override the equals() method. Both String and StringBuffer classes have equals() method.
As per your code,
StringBuffer sb1 = new StringBuffer("String");
StringBuffer sb2 = new StringBuffer("String");
sb1.equals(sb2)checks for the memory address and since sb1 and sb2 refer to two new objects, their memory address is different and hence sb1.equals(sb2) return False.
The following check on StringBuffers will also return False. if(sb1==sb2), returns false.
The contents of StringBuffers sb1 and sb2 can be checked by equals() method like this...
sb1.toString().equals(sb2.toString()) - This check will return True.
You may try the code mentioned below.
public class StringBufferTest
{
public static void main(String Args[])
{
StringBuffer sb1 = new StringBuffer("String");
StringBuffer sb2 = new StringBuffer("String");
if(sb1.equals(sb2))
{
System.out.println("EQUALS");
}
else
{
System.out.println("NOT EQUALS");
}

if(sb1.toString().equals(sb2.toString()))
{
System.out.println("toString EQUALS");
}
else
{
System.out.println("toString NOT EQUALS");
}
if(sb1==sb2)
{
System.out.println(" == EQUALS");
}
else
{
System.out.println("== NOT EQUALS");
}
}
}
The Output of the above code is :
NOT EQUALS
toString EQUALS
== NOT EQUALS
- Suresh Selvaraj


Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="http://www.decontconsulting.com" target="_blank" rel="nofollow">www.decontconsulting.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Buffer
 
Similar Threads
Using equals()
StringBuffer equals() query
String Q
equals method and StringBuffer
Doubt in StirngBuffer