Tuesday, April 5, 2011

Java 5 Feature : Formatted Output: printf

Takes a variable number of arguments
– System.out.printf("Formatting String", arg1, arg2, …);
• Advantages
– Lets you insert values into output without much clumsier String concatenation.
– Lets you control the width of results so things line up
– Lets you control the number of digits after the decimal point in numbers, for consistent-looking output
• Very similar to C/C++ printf function
– If you know printf in C/C++, you can probably use Java's printf immediately without reading any documentation
        • Although some additions in time formatting and locales
– Use String.format to get the equivalent of C's sprintf

No comments:

Post a Comment