java – String cannot be converted to StringBuffer

java – String cannot be converted to StringBuffer

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();

java – String cannot be converted to StringBuffer

Leave a Reply

Your email address will not be published. Required fields are marked *