[ACCEPTED]-Benefits of MessageFormat in Java-messageformat

Accepted answer
Score: 18

Should I change the line of code or stick with the current?

According to your teacher your should.

Perhaps 12 he wants you to learn different approaches 11 for the same thing.

While in the sample 10 you provided it doesn't make much sense, it 9 would be useful when using other types of 8 messages or for i18n

Think about this:

String message = ResourceBundle.getBundle("messages").getString("user.notfound");

throw new UsernameNotFoundException(MessageFormat.format( message , new Object[] {username}));

You 7 could have a messages_en.properties file and a messages_es.properties

The first with 6 the string:

user.notfound=Username '{0}' not found!

And the second with:

user.notfound=¡Usuario '{0}' no encontrado!

Then it would 5 make sense.

Another use of the MessageFormat 4 is described in the doc

 MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
 double[] filelimits = {0,1,2};
 String[] filepart = {"no files","one file","{0,number} files"};
 ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
 form.setFormatByArgumentIndex(0, fileform);

 int fileCount = 1273;
 String diskName = "MyDisk";
 Object[] testArgs = {new Long(fileCount), diskName};


The output with different 3 values for fileCount:

 The disk "MyDisk" contains no files.
 The disk "MyDisk" contains one file.
 The disk "MyDisk" contains 1,273 files.

So perhaps your teacher 2 is letting you know the possibilities you 1 have.

Score: 1

Teachers way allows for easier localisation 2 as you can extract a single string rather 1 than several little bits.

Score: 1

But I don't see the point of using MessageFormat 4 in this situation

In that specific situation it doesn't 3 buy you much. In general, using MessageFormat 2 allows you to externalize those messages 1 in a file. This allows you to:

  • localize the messages by language
  • edit the messages outside without modifying source code
Score: 0

Personally, I would stick with the concatenation 7 way, but it's just a matter of preference. Some 6 people think it's cleaner to write a string 5 with variables as one string, and then pass 4 the params as a list after the string. The 3 more variables you have in the string, the 2 more sense using MessageFormat makes, but 1 you only have one, so it's not a big difference.

Score: 0

One advantage that I see in using MessageFormat is that 5 when you decide to externalize your strings, it would 4 be much easier to build the message and 3 also, it makes more sense to see "Username 2 '{0}' not found!" in your resource file 1 as one string accessed by only one ID.

Score: 0

Of course if you don't need internationalization, it 20 is overhead, but basically the code as the 19 teach wants it is more "internationalizable" (although 18 not actually internationalized as the string 17 is still hard coded).

Since this is a teaching 16 situation, though he may be doing it just 15 to show you how to use those classes rather 14 than as the best way to program for this 13 specific example.

In terms of the best way 12 to program, if internationalization is a 11 requirement, then you need to code for it, if 10 not then don't. I just adds overhead and 9 time (to write the code) for no reason.

Pace 8 the other answers, the importance of MessageFormat 7 for internationalizion is not just that 6 it makes it easier to make an external file. In 5 other languages the location of the parameter 4 may be different in the sentence structure 3 of the messages, so using MessageFormat 2 allows you to change that per language, something 1 that string concatenation would not.

More Related questions