String method Append() : StringBuilder vs StringBuffer

后端 未结 3 1411
小蘑菇
小蘑菇 2021-01-26 10:53

With this code:

  public static void main(String[] args) {

    String s = \"Java\";
    StringBuilder buffer = new StringBuilder(s);
    change(buffer);

               


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-26 11:27

    StringBuilder does have a constructor accepting a String as an argument, and does have a .charAt() method (which it it must implement since it implements CharSequence).

    Conclusion: this is a mishap from the part of your IDE, which did not import the correct StringBuilder. You use another library which has the unfortunate "property" of having implemented a class by the same name -- but not in the same package.

    Go see at the top of your file if the import line is:

    import java.lang.StringBuilder;
    

提交回复
热议问题