I primi due sono esattamente le stesse, dal printf
viene implementato come (source)
public PrintStream printf(String format, Object ... args) {
return format(format, args);
}
Gli ultimi due sono anche esattamente lo stesso, poiché String.format
viene implementato come (source)
public static String format(String format, Object ... args) {
return new Formatter().format(format, args).toString();
}
Infine, il 2 ° e il 4 ° sono più o meno gli stessi, come si può vedere dall'implementazione di PrintStream.format
(source). Sotto il cofano, crea anche un nuovo Formatter
(se necessario) e chiama format
su quello Formatter
.
public PrintStream format(String format, Object ... args) {
try {
synchronized (this) {
ensureOpen();
if ((formatter == null)
|| (formatter.locale() != Locale.getDefault()))
formatter = new Formatter((Appendable) this);
formatter.format(Locale.getDefault(), format, args);
}
} catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
} catch (IOException x) {
trouble = true;
}
return this;
}
fonte
2013-05-16 09:29:39