Java : StandardCharsets with Examples

StandardCharsets (Java SE 22 & JDK 22) with Examples.
You will find code examples on most StandardCharsets methods.


Summary

Constant definitions for the standard charsets. These charsets are guaranteed to be available on every implementation of the Java platform.

Class diagram

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

ISO Latin Alphabet No. 1, also known as ISO-LATIN-1.

final var charset = StandardCharsets.ISO_8859_1;
System.out.println(charset); // ISO-8859-1

static final Charset US_ASCII

Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.

final var charset = StandardCharsets.US_ASCII;
System.out.println(charset); // US-ASCII

static final Charset UTF_16

Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark.

final var charset = StandardCharsets.UTF_16;
System.out.println(charset); // UTF-16

static final Charset UTF_16BE

Sixteen-bit UCS Transformation Format, big-endian byte order.

final var charset = StandardCharsets.UTF_16BE;
System.out.println(charset); // UTF-16BE

static final Charset UTF_16LE

Sixteen-bit UCS Transformation Format, little-endian byte order.

final var charset = StandardCharsets.UTF_16LE;
System.out.println(charset); // UTF-16LE

static final Charset UTF_32

Thirty-two-bit UCS Transformation Format, byte order identified by an optional byte-order mark.

final var charset = StandardCharsets.UTF_32;
System.out.println(charset); // UTF-32

static final Charset UTF_32BE

Thirty-two-bit UCS Transformation Format, big-endian byte order.

final var charset = StandardCharsets.UTF_32BE;
System.out.println(charset); // UTF-32BE

static final Charset UTF_32LE

Thirty-two-bit UCS Transformation Format, little-endian byte order.

final var charset = StandardCharsets.UTF_32LE;
System.out.println(charset); // UTF-32LE

static final Charset UTF_8

Eight-bit UCS Transformation Format.

final var charset = StandardCharsets.UTF_8;
System.out.println(charset); // UTF-8

Related posts

To top of page