Java 1.4 and earlier have no equivalent of printf( ). Even Java 6 has no equivalent of scanf( ) . Part of the reason is that Java didn support the variable-length argument lists on which these functions depend until Java 5. However, the real reason Java didn have equivalents to Cs formatted I/O routines is a difference in philosophy. Cs printf( ), scanf( ), and related functions combine number formatting and parsing with I/O in an inflexible manner. Java separates number formatting and I/O into separate packages and by so doing produces a much more general and powerful system.
More than one programmer has attempted to recreate printf( ) and scanf( ) in Java. However, overloading the + signs for string concatenation is easily as effective, probably more so, since it doesn share the problems of mismatched argument lists. For example, which is clearer to you? This:
System.out.printf("%s worked %d hours at $%d per/hour for a total of %d dollars. ", hours, salary, hours*salary);
System.out.println(employee + " worked " + hours + " hours at $" + salary + "per/hour for a total of $" + hours*salary);
Id argue that the second is clearer. Among other advantages, it avoids problems with mismatched format strings and argument lists. (Did you notice that an argument is missing from the previous printf( ) statement?) On the flip side, the format string approach is a little less prone to missing spaces. (Did you notice that the println( ) statement would print pay scales as "$5.35per/hour" rather than "$5.35 per/hour"?) However, this is only a cosmetic problem and is easily fixed.
The real advantage of the printf( )/scanf( ) family of functions is not the format string. Its number formatting:
printf( "%s worked %4.1d hours at $%6.2d per/hour for a total of %8.2d dollars. ", employee, hours, salary, hours*salary);
Javas been able to format numbers like this since version 1.1. However, its done so with the java.text.NumberFormat class rather than with embedded control codes in format strings.
Streams in Memory
The File System
Working with Files
File Dialogs and Choosers
Character Sets and Unicode
Readers and Writers
Formatted I/O with java.text
The Java Communications API
The J2ME Generic Connection Framework
Authors: Elliotte Rusty Harold
Similar book on Amazon
Lean Six Sigma for Service : How to Use Lean Speed and Six Sigma Quality to Improve Services and Transactions