This
StringBuffer sb = new StringBuffer("Welcome");
String st = sb + "";
will result more or less in
StringBuffer sb = new StringBuffer("Welcome");
StringBuilder builder = new StringBuilder();
builder.append((sb == null) ? "null" : sb.toString());
builder.append("");
String st = builder.toString();
Answer from Sotirios Delimanolis on Stack OverflowThis
StringBuffer sb = new StringBuffer("Welcome");
String st = sb + "";
will result more or less in
StringBuffer sb = new StringBuffer("Welcome");
StringBuilder builder = new StringBuilder();
builder.append((sb == null) ? "null" : sb.toString());
builder.append("");
String st = builder.toString();
If we compile them into two methods in java
private StringBuffer sb = new StringBuffer("Welcome");
public String testToString() {
return sb.toString();
}
public String testAppend() {
return sb + "";
}
and then use javap -v, we get
public java.lang.String testToString();
descriptor: ()Ljava/lang/String;
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: getfield #19 // Field sb:Ljava/lang/StringBuffer;
4: invokevirtual #27 // Method java/lang/StringBuffer.toString:()Ljava/lang/String;
7: areturn
LineNumberTable:
line 10: 0
LocalVariableTable:
Start Length Slot Name Signature
0 8 0 this Lcom/stackoverflow/Question;
public java.lang.String testAppend();
descriptor: ()Ljava/lang/String;
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: new #31 // class java/lang/StringBuilder
3: dup
4: invokespecial #33 // Method java/lang/StringBuilder."<init>":()V
7: aload_0
8: getfield #19 // Field sb:Ljava/lang/StringBuffer;
11: invokevirtual #34 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
14: invokevirtual #38 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
17: areturn
LineNumberTable:
line 14: 0
LocalVariableTable:
Start Length Slot Name Signature
0 18 0 this Lcom/stackoverflow/Question;
So appending appears to be slightly less efficient as it uses an extra bit of stack and contains more instructions.
1) Create StringBuffer object and pass it to addFilter().
2) Convert StringBuffer object return by addFilter() into String by toString().
keyword = addFilter(new StringBuffer(keyword)).toString();
String is final and immutable too.
Hence, this class cannot be extended or inherited by any other class. So, there is no IS-A Relationship between these two classes. String can never be converted or referenced to any other Class except java.lang.Object directly.
But StringBuffer provides a Constructor through which you can convert a String into a StringBuffer Object.
public java.lang.StringBuffer(java.lang.String); //pass string as an argument. StringBuffer object will you get.
In your case, try this code:
addFilter(new StringBuffer(keyword)).toString();
It is simple to understand.
In the first case, the value is returned and is being referenced by a variable so that you can make use of that value later on.
In the second case, the value is returned just like before but it is not being referenced by any variable. Thus, the value simply goes into waste and can not be used or manipulated later on.
sb.toString();//what is happening here?
You are converting the StringBuilder object to String which is good but you are not storing the return value to a String type and using it later in your equals call. You should do it the following way:
String reversedString = sb.toString();
if(s.equals(reversedString )){
Or simply
if(s.equals(sb.toString())){
Right now you are comparing s with sb using the equals method of String class. This method returns false if the object passed in as an argument is not an instance of String class. Since StringBuilder object sb is not an instance of String, the equals method returns false.