I was wondering why my professor would type
System.out.println("%d", myRank);
instead of
System.out.println(myRank);
from my point of view the latter is inherently more efficient and has the same effect
I've tried both and they perform the same, so I am quite confused as to why the former is used at all
First of all,
System.out.println("%d", myRank);will not compile.You can use
System.out.printforString.format(...)insideprintfFor a single argument, as you mentioned, it does not make much sense. The value of
String.format(orprintf) can be seen when you have string concatenation and it makes the code more readable and easier to maintain.For example, you can print the following message:
Instead of: