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


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "string(buffer) comparision..." Watch "string(buffer) comparision..." New topic
Author

string(buffer) comparision...

sam mathew
Greenhorn

Joined: Dec 15, 2000
Posts: 13
I saw the following question in a mock exam site...
public class Test1 {
public static void main( String [] args ) {
StringBuffer sb1 = new StringBuffer("Amit");
StringBuffer sb2= new StringBuffer("Amit");
String ss1 = "Amit";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(ss1));
System.out.println("Poddar".substring(3));
}
}
The result when I run this is false,false,false,dar..
But,what I expected was false,true,false,dar..
can someone clarify...
Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
Hi Sam,
StringBuffer does not have any equals() method for itself. It inherits this method from that of java.lang.Object. The following is an extract taken from javadoc api specification:
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).
So, sb1.equals(sb2) will return true only if both these point to the same object. Hence, when their references are the same, you will have the value "true" returned.
Hope this solves your problem.
Regds,
Aparna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string(buffer) comparision...