Isambe Isambe - 5 months ago 15
Java Question

Java printf cheat: Is this legal?

This cheat compiles and runs just fine in NetBeans. However, it seems like it's probably not legal, for a couple of different reasons. Or is it? It could make code a bit more compact in some situations. Just curious.

System.out.printf("%-20d John Smith %n", 12);


...which gives me:

12 John Smith

Answer

It's perfectly legal, -20 left aligns twenty spaces and %n is a newline.