Class Utf8DefaultCharsetExamples

java.lang.Object
net.jrodolfo.java_evolution.java18.Utf8DefaultCharsetExamples

public class Utf8DefaultCharsetExamples extends Object
Demonstrates UTF-8 as the default charset, standardized in Java 18.

Before Java 18, APIs that used the default charset could behave differently on different machines because the default came from the operating system and locale. That made simple text examples harder to reason about and made file exchange bugs easy to create accidentally. Java 18 made UTF-8 the standard default so common text handling is predictable across platforms.

The examples still keep the lesson visible: defaults are now safer, but explicit charsets are still best when a file format, protocol, or external system requires one.

  • Constructor Details

    • Utf8DefaultCharsetExamples

      public Utf8DefaultCharsetExamples()
  • Method Details

    • defaultCharset

      public Charset defaultCharset()
      Reads the JVM default charset.
      Returns:
      the default charset
    • defaultCharsetIsUtf8

      public boolean defaultCharsetIsUtf8()
      Checks whether the default charset is UTF-8.
      Returns:
      whether the default charset is StandardCharsets.UTF_8
    • bytesWithDefaultCharset

      public byte[] bytesWithDefaultCharset(String text)
      Encodes text using the default charset, which Java 18 standardized as UTF-8.
      Parameters:
      text - the text to encode
      Returns:
      encoded bytes