The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
Many programs are not internationalized when first written. These programs may have started as prototypes, or perhaps they were not intended for international distribution. If you must internationalize an existing program, take the following steps:
Text messages are the most obvious form of data that varies with culture. However, other types of data may vary with region or language. The following list contains examples of culturally dependent data:
Translation is costly. You can help reduce costs by isolating the text that must be translated in ResourceBundle
objects. Translatable text includes status messages, error messages, log file entries, and GUI component labels. This text is included into programs that haven't been internationalized. You need to locate all occurrences of included text that is displayed to end users. For example, you should clean up code like this:
String buttonLabel = "OK"; // ... JButton okButton = new JButton(buttonLabel);
See the section Isolating Locale-Specific Data for details.
Compound messages contain variable data. In the message "The disk contains 1100 files." the integer 1100 may vary. This message is difficult to translate because the position of the integer in the sentence is not the same in all languages. The following message is not translatable, because the order of the sentence elements is fixed by concatenation:
Integer fileCount; // ... String diskStatus = "The disk contains " + fileCount.toString() + " files";
Whenever possible, you should avoid constructing compound messages, because they are difficult to translate. However, if your application requires compound messages, you can handle them with the techniques described in the section Messages.
If your application displays numbers and currencies, you must format them in a locale-independent manner. The following code is not yet internationalized, because it will not display the number correctly in all countries:
Double amount; TextField amountField; // ... String displayAmount = amount.toString(); amountField.setText(displayAmount);
You should replace the preceding code with a routine that formats the number correctly. The Java programming language provides several classes that format numbers and currencies. These classes are discussed in the section Numbers and Currencies.
Date and time formats differ with region and language. If your code contains statements like the following, you need to change it:
Date currentDate = new Date(); TextField dateField; // ... String dateString = currentDate.toString(); dateField.setText(dateString);
If you use the date-formatting classes, your application can display dates and times correctly around the world. For examples and instructions, see the section Dates and Times.
The following code tries to verify that a character is a letter:
char ch; // This code is incorrect if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
Watch out for code like this, because it won't work with languages other than English. For example, the if
statement misses the character ü in the German word Grün.
The Character
comparison methods use the Unicode standard to identify character properties. Thus you should replace the previous code with the following:
char ch; // ... if (Character.isLetter(ch))
For more information on the Character
comparison methods, see the section
Checking Character Properties.
When sorting text you often compare strings. If the text is displayed, you shouldn't use the comparison methods of the String
class. A program that hasn't been internationalized might compare strings as follows:
String target; String candidate; // ... if (target.equals(candidate)) { // ... if (target.compareTo(candidate) < 0) { // ...
The String.equals
and String.compareTo
methods perform binary comparisons, which are ineffective when sorting in most languages. Instead you should use the Collator
class, which is described in the section
Comparing Strings.
Characters in the Java programming language are encoded in Unicode. If your application handles non-Unicode text, you might need to translate it into Unicode. For more information, see the section Converting Non-Unicode Text.