• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String.format vs java.text.MessageFormat

 
Leah O'Connor
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic