Quick revision
The short answer
We plan it as a migration, not a JVM bump. First we inventory every place that relies on the default charset, such as FileReader, FileWriter, new String(bytes) and getBytes(), using static analysis like Error Prone's DefaultCharset check. Then we identify the services whose behaviour can actually change, those on Windows or non-UTF-8 locales, and test them with real data, using -Dfile.encoding=COMPAT only as a temporary bridge. The resulting standard is simple: always pass a charset explicitly, even though UTF-8 is now the default.
What to remember
In this answer, we planned the move to a UTF-8 default as a migration: inventory the default-charset calls with static analysis such as Error Prone's DefaultCharset check, rank services by whether their old default differed and whether they handle non-ASCII text, test the risky ones with real data, and use -Dfile.encoding=COMPAT only as a temporary bridge. The standard that results is to pass a charset explicitly on every conversion, enforced by the build.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We plan it as a migration, not a JVM bump. First we inventory every place that relies on the default charset, such as FileReader, FileWriter, new String(bytes) and getBytes(), using static analysis like Error Prone's DefaultCharset check. Then we identify the services whose behaviour can actually change, those on Windows or non-UTF-8 locales, and test them with real data, using -Dfile.encoding=COMPAT only as a temporary bridge. The resulting standard is simple: always pass a charset explicitly, even though UTF-8 is now the default.
Why this question is asked
A platform change that is a one-line note in release notes can still break a fleet. JEP 400 made UTF-8 the default charset in Java 18, and for most Linux services nothing visible happens, which is exactly what makes it risky: the few services that change behaviour do so silently, by writing or reading different bytes. Interviewers ask this at a senior level to see how we turn a technical change into a plan with inventory, risk ranking, testing, rollback and a lasting rule. In this answer, we will recap what changed, walk through finding the affected code, deciding which services are really at risk, running the migration safely, and writing the coding standard that comes out of it.
1A Quick Recap of What Changed
Before Java 18, the default charset was taken from the operating system locale when the JVM started. It was UTF-8 on most Linux servers and macOS, a legacy code page such as Windows-1252 on Windows, and sometimes US-ASCII in minimal containers with no locale set. Since Java 18 the default is UTF-8 on every platform. Any API that encodes or decodes text without being told a charset uses that default, so its output can change on upgrade.
Note that the change is only visible where the old default was not UTF-8. A Linux service whose locale was already UTF-8 behaves exactly as before. That observation is the key to sizing the work.
2Inventory the Code That Relies on Defaults
We cannot reason about risk until we know where defaults are used. The usual suspects are constructors and methods with no charset parameter: FileReader and FileWriter without a Charset, InputStreamReader and OutputStreamWriter without one, new String(bytes), String.getBytes(), PrintStream and PrintWriter on files, Scanner on an InputStream, and older library helpers that do the same internally.
Searching by hand misses cases, so we use static analysis. Error Prone has a DefaultCharset check that flags these calls at compile time, and other analysers such as SpotBugs have similar rules. We run the check across all repositories in report mode first, which gives us a list of findings per service. That list is the inventory, and it also tells us roughly how much code needs to change.
3Rank Which Services Can Actually Change
A finding in code is only a real risk where the old default differed from UTF-8 and the data contains non-ASCII text. So for each service we record where it runs (Windows hosts, containers without a UTF-8 locale, Linux hosts with UTF-8), what its default charset is today, which we can read from Charset.defaultCharset() or the file.encoding and native.encoding properties at startup, and what text it handles: names, addresses, free text, or only ASCII identifiers.
This usually shrinks the problem a lot. A team of 120 services might find that 100 run on Linux with UTF-8 and can upgrade with a quick check, 15 have findings but handle only ASCII, and 5, typically Windows batch jobs and integrations with legacy partners, are genuinely at risk. Those 5 get careful attention.
- Uses default-charset APIs?Yescheck its platformNoupgrade normally
- Old default was UTF-8?Yeslow risk, fix code when convenientNocheck the data
- Handles non-ASCII text?Yeshigh risk: test with real dataNomedium risk: fix code before upgrade
3 steps in order. Play them, or step through them one at a time.
4Test With Real Data and Migrate in Stages
For the high-risk services we capture representative real inputs and outputs, with names in several languages and files from partners, and compare the bytes produced on the old JVM and the new one. Wherever the bytes differ we decide on purpose which encoding is correct. Often UTF-8 is what everyone wanted and the old output was the bug; sometimes a partner genuinely needs Windows-1252, and then the code must say so explicitly.
To decouple the JVM upgrade from those decisions, Java 18 and later accept -Dfile.encoding=COMPAT, which restores the locale-based default. A service that is not ready can upgrade with that flag, run on the new JVM with its old behaviour, and then drop the flag once its code passes charsets explicitly. We treat the flag as debt: each use has an owner and a removal date, and a dashboard shows which services still carry it.
Files already on disk need a decision too. Old archives written in a code page will be misread by code that now defaults to UTF-8, so readers of historic data must name the old charset or the data must be converted once.
5The Coding Standard That Results
The lesson of the migration is not to rely on UTF-8 now being the default; it is not to rely on defaults at all. So the standard says: every conversion between bytes and text passes a charset explicitly, normally StandardCharsets.UTF_8, and any other charset is named and commented with the reason, such as a partner's file format. We enforce it by turning the DefaultCharset check from a warning into an error in the shared build configuration, so the rule does not depend on reviewers remembering it.
Explicit charsets cost a few characters per call. In return, the code documents its data format, behaves the same on every platform, on every JVM version and with every startup flag, and the next time a default changes, nothing in the fleet notices.
6Follow-up questions
Why insist on explicit charsets if UTF-8 is now the default anyway?
Because the code then states its file format instead of inheriting it from the environment. A COMPAT flag in some startup script, a future platform change or code copied into a tool that runs on an older JVM would otherwise change the bytes silently. Explicit charsets also make the standard easy to enforce automatically, since any call without one is simply an error.
How do you get teams to actually do the work?
We make it cheap and visible. The platform team provides the shared build rule, a short migration guide and a script that runs the check and prints findings per repository. Each team gets its own ranked list, and only the high-risk services need a real test plan. We track the COMPAT flag and remaining findings on a fleet dashboard and agree a date after which the flag is no longer allowed.
7Conclusion
In this answer, we planned the move to a UTF-8 default as a migration: inventory the default-charset calls with static analysis such as Error Prone's DefaultCharset check, rank services by whether their old default differed and whether they handle non-ASCII text, test the risky ones with real data, and use -Dfile.encoding=COMPAT only as a temporary bridge. The standard that results is to pass a charset explicitly on every conversion, enforced by the build.
Open with
I would treat it as a migration with an inventory, a risk ranking and a standard at the end. First, find every place that relies on the default charset: FileReader, FileWriter, InputStreamReader without a charset, new String(bytes), getBytes and similar.
Then, if they want more
I would not rely on grep; I would run Error Prone's DefaultCharset check across all repositories in warning mode and use its findings as the inventory. Second, rank the risk.
The change only matters where the old default was not UTF-8, so Windows hosts and containers without a UTF-8 locale, and only where the data has non-ASCII text like customer names. That usually reduces a big fleet to a handful of services that really need attention.
For those I would capture real data and compare the bytes on the old and new JVM, and decide on purpose which encoding is right, because sometimes a partner really needs Windows-1252 and the code must say so.
To separate the JVM upgrade from those fixes, a service can run temporarily with -Dfile.encoding=COMPAT, but every use gets an owner and a removal date. I would also check readers of old files written in the old code page.
The standard that comes out is: always pass a charset explicitly, even UTF-8, and the DefaultCharset check becomes a build error, so the code never depends on defaults again.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 18
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After moving to Java 18 or later, the default charset is UTF-8 everywhere. Your fleet...": https://scrivio-interview-library.netlify.app/java/io-and-nio/9c0116b1fe/