7 Examples of String.format() and printf() in Java - Formatting Text and String in Java

You can format String in Java either by using String.format() method or by using System.printf() method, both uses TextFormat class to format String input internally. Both of these methods are inspired from C-Style printf() method and formatting instruction are very similar to C e.g. %s for String, %d for numeric, %f for floating point value and %n for new lines etc, but it is not exactly same as C. Some customization have been made to accommodate some Java language features. Java's String formatting is also more strict than C's, for example, if a conversion is incompatible with a flag then Java throws an Exception ( java.util.IllegalFormatConversionException), while in C, incompatible flags are silently ignored.
Read more »