Default Charset Is UTF-8
Confirm that Charset.defaultCharset is UTF-8 since JDK 18 regardless of platform, so charset-less getBytes and new String behave identically everywhere.
Code
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class EncodingBaseline {
public static void main(String[] args) {
// JDK 18 起(JEP 400):default charset 恒为 UTF-8,与平台无关
Charset def = Charset.defaultCharset();
System.out.println("defaultCharset: " + def);
System.out.println("is utf-8: " + (def == StandardCharsets.UTF_8));
System.out.println("file.encoding: " + System.getProperty("file.encoding"));
// 不带 charset 参数的 getBytes / new String 走同一套 default charset
byte[] bytes = "naïve 中文".getBytes();
System.out.println("utf-8 cjk bytes: " + "中".getBytes().length);
System.out.println("roundtrip: " + new String(bytes).equals("naïve 中文"));
}
}How it works
The code’s whole job belongs to the class EncodingBaseline and the function main.
Caveats
- No dependencies section needed; the standard library is the whole story.
Using it
Signature: public static void main(String[] args)
The run emits 5 labeled sections — defaultCharset:, is utf-8:, file.encoding:, utf-8 cjk bytes:, roundtrip: — one per step.
Key statements
Charset def = Charset.defaultCharset();
Interactive Tools for This Topic
ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.ASCII85 EncoderEncode and decode ASCII85 (Base85), the Adobe variant with z zero-compression, handling arbitrary bytes and Unicode.Base32 Encoder & DecoderEncode and decode RFC 4648 Base32 with padding, fully UTF-8 safe so Unicode text round-trips without corruption. Nothing leaves your browser.Base45 Encoder and DecoderEncode and decode text with the Base45 alphabet defined in RFC 9285, with UTF-8 support, padding-free output, and clear validation errors.
FAQ
- What does Default Charset Is UTF-8 print when run?
- No setup needed: run the file and look for labeled lines like `defaultCharset:`, `is utf-8:`, `file.encoding:` in the output.
Report a problem with this exampleOpens a pre-filled issue on GitHub.