Java : StandardCharsets with Examples
StandardCharsets (Java SE 22 & JDK 22) with Examples.
You will find code examples on most StandardCharsets methods.
Summary
final var utf8 = StandardCharsets.UTF_8;
System.out.println(utf8); // UTF-8
final var utf16 = StandardCharsets.UTF_16;
System.out.println(utf16); // UTF-16
final var text = "abc♪";
System.out.println(text); // abc♪
final var encoded1 = text.getBytes(utf8);
// [97, 98, 99, -30, -103, -86]
System.out.println(Arrays.toString(encoded1));
final var encoded2 = text.getBytes(utf16);
// [-2, -1, 0, 97, 0, 98, 0, 99, 38, 106]
System.out.println(Arrays.toString(encoded2));
Fields
static final Charset ISO_8859_1
final var charset = StandardCharsets.ISO_8859_1;
System.out.println(charset); // ISO-8859-1
static final Charset US_ASCII
final var charset = StandardCharsets.US_ASCII;
System.out.println(charset); // US-ASCII
static final Charset UTF_16
final var charset = StandardCharsets.UTF_16;
System.out.println(charset); // UTF-16
static final Charset UTF_16BE
final var charset = StandardCharsets.UTF_16BE;
System.out.println(charset); // UTF-16BE
static final Charset UTF_16LE
final var charset = StandardCharsets.UTF_16LE;
System.out.println(charset); // UTF-16LE
static final Charset UTF_32
final var charset = StandardCharsets.UTF_32;
System.out.println(charset); // UTF-32
static final Charset UTF_32BE
final var charset = StandardCharsets.UTF_32BE;
System.out.println(charset); // UTF-32BE
static final Charset UTF_32LE
final var charset = StandardCharsets.UTF_32LE;
System.out.println(charset); // UTF-32LE
static final Charset UTF_8
final var charset = StandardCharsets.UTF_8;
System.out.println(charset); // UTF-8