String formatting in a bit more complex cases

The good thing about using a writer is flexibility and not having to either pre-compute the final length of the string, or repeatedly reallocate the string that is being accumulated in something like an ArrayList.
If you are printing to stdout or writing to a file the append to the output can be done directly without all the temporary additional allocations that would happen if you turn it into one big string first and print it then.

When such a format function is defined on a type and you try to print that instance, then that function is automatically invoked with a writer, you don’t have to manually provide it.

Also take a look at this post:

2 Likes