sebastiandaschner blog


UTF-8 charset as constant in Java

#java wednesday, december 02, 2015

Since Java 7 you can access the often used UTF-8 charset as constant value.

This charset and some more are avaliable in java.nio.charset.StandardCharsets:

public class CharsetTest {

    @Test
    public void testUtf8() {
        byte[] bytes = {77, -61, -68, 110, 99, 104, 101, 110};

        String actual = new String(bytes, StandardCharsets.UTF_8);

        assertThat(actual, is("München"));
    }
}

 

Found the post useful? Subscribe to my newsletter for more free content, tips and tricks on IT & Java: