Why does Java's String.getBytes() uses “ISO-8859-1”

前端 未结 4 1605
广开言路
广开言路 2020-12-01 08:34

from java.lang.StringCoding :

String csn = (charsetName == null) ? \"ISO-8859-1\" : charsetName;

This is what is used from Java.lang.getByt

4条回答
  •  眼角桃花
    2020-12-01 09:03

    Elaborate on Skeet's answer (which is of course the correct one)

    In java.lang.String's source getBytes() calls StringCoding.encode(char[] ca, int off, int len) which has on its first line :

    String csn = Charset.defaultCharset().name();
    

    Then (not immediately but absolutely) it calls static byte[] StringEncoder.encode(String charsetName, char[] ca, int off, int len) where the line you quoted comes from - passing as the charsetName the csn - so in this line the charsetName will be the default charset if one exists.

提交回复
热议问题