• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String.format vs java.text.MessageFormat

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't touch me. And dont' touch this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic