StringBuffer

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Yes, this would work.

StringBuffer

The reversed String could be built up character by character in an array. Since arrays can be altered at any time, only one new object would be constructed. This is essentially the idea of the StringBuffer class.

A StringBuffer object holds characters that can be changed by appending and by inserting characters. Also, a StringBuffer object includes many useful character manipulation methods. Constructors for StringBuffer are:

StringBuffer constructors
public StringBuffer() create an empty StringBuffer
public StringBuffer(int capacity) create a StringBuffer with initial room for capacity characters
public StringBuffer(String st) create a StringBuffer containing the characters from st

If you insert characters into a StringBuffer, it automatically grows to the length you need.

QUESTION 6:

Why would you ever want a StringBuffer with length zero?