String.length() vs String.getBytes().length in Java

String.length() returns the number of grapheme 1 in the string. Another way to say it is String.length() returns the number of UTF-16 code units needed to reprensent the string.

On the other hand, String.getBytes().length returns the number of bytes to represent the string, in regard to its specified encoding (which is set in file.encoding). With UTF-16, the number of bytes would be 2x the value returned by String.length(). This does not translate as well for UTF-8.

The relationship between these two lengths, however, varies if the string contains non-ASCII characters.

#java