Simply put, the main difference is in the format string:
MessageFormat.format() takes the position of the arguments (for example, {0} , {1} ). Example:
"This is year {0}!"
The developer does not need to worry about the types of arguments, since they are most often recognized and configured in accordance with the current Locale .
Format string
String.format() accepts arguments of type argument (for example, %d for numbers, %s for strings). Example:
"This is year %d!"
String.format() usually gives you more control over how the argument is displayed, thanks to many parameters that you can specify with a type specifier. For example, the format string "%-6.2f" indicates the display of the left- "%-6.2f" floating-point number with min. 6 characters wide and 2 decimal places precision.
Just look at the javadoc of both methods for more details.
Adam Dyga Feb 18 '13 at 14:12 2013-02-18 14:12
source share