aspose file tools*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "String.format vs java.text.MessageFormat" Watch "String.format vs java.text.MessageFormat" New topic
Author

String.format vs java.text.MessageFormat

Leah O'Connor
Greenhorn

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
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: String.format vs java.text.MessageFormat