String format and printf Example
How to format String in Java is most common problem developer encounter because classic System.out.println() doesn’t support formatting of String while printing on console. For those who doesn’t know What is formatted String ? here is a simple definition, Formatted String is a String which not only display contents but also display it in a format which is widely accepted like including comma while displaying large numbers e.g. 100,000,000 etc. Displaying formatted String is one of need for modern GUI application and thankfully Java has good support for formatting String and all other types like Integers, Double and Date. How to format a String in Java is never as easy as it has been since Java 1.5 which along-with front line features like Generics, Enum, Autoboxing and Varargs also introduces several utility method to support rich formatting of String in Java. prior to Java 5 java programmer relies java.text API for all there formatting need but with Java 5 we have now two more convenient way to format String in Java. JDK 1.5 has added format() method in java.lang.String class and provided a printf() method in PrintStream class for printing formatted output in console. printf() method is similar to C programming language printf() method and allows programmer to print formatting string directly to console, which makes System.out.printf() better alternative of System.out.println() method. Both format() and printf() are overloaded method to support Locale specific formatting.
By the way this is the third article about formatting in Java , earlier we have seen Decimal Format examples and DateFormat examples for formatting numbers and dates in Java.
How String.format()or printf()works in Java
String.format() and System.out.printf() both works similarly and if you see the signature of both method they also accept variable arguments . Both take minimum two parameters, first of them is formatting instruction and other was actual String or anything which needs to be formatted. Java formatting instructions are both powerful and flexible and allows you to generate formatted String on many different format. Its worth to understand format of "formatting instruction" to take full benefit of String.format() method because this is the only tricky part of String formatting specially if you have not used printf() in past. I have seen developer struggle to understand the formatting behavior because of lack of knowledge of different formatting options available in Java.This is how we specify formatting instruction in Java :
String.format "%[argument number] [flags] [width] [.precision] type"
Now let's see what is meaning of each part of formatting instruction. "%" is a special character in formatted String and it denotes start of formatting instruction. String.format() can support multiple formatting instruction with multiple occurrence of "%" character in formatting instruction.
"argument number" is used to specify correct argument in case multiple arguments are available for formatting. "flags" is another special formatting instruction which is used to print String in some specific format for example you can use flag as "," to print comma on output. "width" formatting option denotes minimum number or character will be used in output but in case if number is larger than width then full number will be displayed but if its smaller in length then it will be be padded with zero. "precision" is using for print floating point formatted String, by using precision you can specify till how many decimal a floating point number will be displayed in formatted String. "type" is the only mandatory formatting option and must always comes last in format String also input String which needs to be formatted must be with same type specified in "type" parameter. for example you can not input a floating point number if you have specified "type" as decimal integer "%d", that will result in error. Now let's see an example of String format() method to understand these formatting option better:
format ( "%,6.2f", 124.000)
In above example of String.format() method flag is comma "," , width is 6 and precision is upto 2 decimal point and type is float.
String format Example in Java
In this section we will see different examples to format String in Java. We will see how we can format numbers and dates. Introduce decimal points and aligning number left or right etc. One of the common application of format() is to print leading zero in a number as shown in this Java program example:
Difference between the printf and format methods in Java
printf() and format()both methods are used to format String in Java and more or less similar. printf()is more close to C programming language because of identical name used in C programming language, Anyone who has work in C previously can easily start with this printf() method also its look more as a replacement of System.out.println(). if you don't want to print just want a formatted string for any other purpose String format() method is a way to go. In summary you can say that printf()writes on stdout while format() return you a formatted string.
We have already seen String format examples with both format and printf method. In short formatting is easier in Java and it provides several classes like DateFormat, NumberFormat etc which can also be used to format Date and numbers.
String related post from Javarevisited, you may like: