This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes String.format vs java.text.MessageFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String.format vs java.text.MessageFormat" Watch "String.format vs java.text.MessageFormat" New topic

String.format vs java.text.MessageFormat

Leah O'Connor

Joined: Dec 10, 2007
Posts: 5
I believe String.format is new with Java 1.5 but java.text.MessageFormat was in 1.4.2. Both allow you to format strings, but do it very differently. String.format looks a lot like C++ printf. Any idea why Java 5 added String.format if MessageFormat was already available? Besides the fact that String.format looks a little easer to use to an old C programmer, are there other reasons for using it instead of MessageFormat? My next project will have a lot of complex text formatting, and I am wondering if being able to use String.format is a reason to move up to Java 1.5 from 1.42. If anyone has experience with either of these classes, please let me know your opinions.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38075
Yes, String.format was introduced in Java5, along with Scanner and Formatter; if I remember correctly, String.format uses Formatter.

The % tags (which date back to C in the early 1970s, maybe even farther back look for "the March of Progress") are much easier to use than the DecimalFormat classes which appear to be called by MessageFormat. Most of us who remember DecimalFormat were only too pleased to be able to use % tags. For details of the % tags, look under Formatter in the API documentation.
I agree. Here's the link:
subject: String.format vs java.text.MessageFormat
Similar Threads
Add varying whitespace to end of string
String.format questions
Custom Strings
Preformatted Output
phone number formatter in struts2