With this code:
public static void main(String[] args) {
String s = "Java";
StringBuilder buffer = new StringBuilder(s);
change(buffer);
System.out.println("What's strBuf.charAt(5)? " + strBuf.charAt(3));
System.out.println(buffer);
}
private static void change(StringBuilder buffer) {
buffer.append(" and HTML");
}
When I run the code using StringBuilder I get error message
The constructor StringBuilder(String) is undefined
The method charAt(int) is undefined for the type StringBuilder
Tried StringBuffer instead and it works. The content of the StringBuffer object is compiled to "Java and Eclipse.."
public static void main(String[] args) {
String s = "Java";
StringBuffer strbuf = new StringBuffer(s);
change(strbuf);
System.out.println("The Stringbuffer.charAt(5) is ? " + strbuf.charAt(3));
System.out.println(strbuf);
}
private static void change(StringBuffer strbuf) {
strbuf.append(" and Eclipse");
}
}