liveBook · Manning

Default system locale

As explained in 100 Java Mistakes and How to Avoid Them

The default locale is the locale selected by the JVM from operating-system preferences, environment variables, or JVM system properties when no locale is specified explicitly.

The default system locale is selected by the JVM from operating-system preferences, environment variables such as LANG, or JVM system properties when code does not specify a locale. Locale-sensitive string and number operations can therefore change behavior across machines. section 6.2 section 6.11

A common mistake is using argument-free toUpperCase() or toLowerCase() for identifiers, tags, or keys. Turkish distinguishes dotted and dotless I, so a conversion that works under an English locale can produce a different result under Turkish. Case conversion can also change string length: Straße becomes STRASSE under German uppercase conversion. section 6.2

Locale-dependent number formatting

String.format("%.2f", 1.234) may produce a period in a US locale and a comma in a German locale. If that result is later passed to Double.parseDouble(), the program can reject valid displayed input. Grouping separators can create an even worse failure by silently reinterpreting the number. section 6.3

Avoid the mistake with explicit and consistent locale choices:

  • Specify the intended locale for case conversion when processing language-independent identifiers or structured data. section 6.2
  • Do not pair locale-aware formatting with locale-independent parsing; use the same NumberFormat instance for both formatting and parsing when supporting localized numbers. section 6.3
  • Test behavior under different locales, including Turkish and German, and use static analysis to locate argument-free case conversions where supported. section 6.2

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains JVM locale selection, case-conversion edge cases, formatting mismatches, and consistent formatting-parsing practices through focused examples.
Examples
Uses Turkish and German case conversion, localized decimal and grouping separators, and a Swing number-input example.
What you can do after
Apply it by choosing explicit locales and keeping number formatting and parsing within the same locale-aware API.
How it connects
Connects JVM system properties, operating-system preferences, case conversion, String.format, NumberFormat, and Double.parseDouble.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.