Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A string is a list of <em>characters</em> (i.e. code points). The number of bytes taken to represent the string <strong>depends entirely on which encoding you use to turn it into bytes</strong>.</p> <p>That said, you can turn the string into a byte array and then look at its size as follows:</p> <pre><code>// The input string for this test final String string = "Hello World"; // Check length, in characters System.out.println(string.length()); // prints "11" // Check encoded sizes final byte[] utf8Bytes = string.getBytes("UTF-8"); System.out.println(utf8Bytes.length); // prints "11" final byte[] utf16Bytes= string.getBytes("UTF-16"); System.out.println(utf16Bytes.length); // prints "24" final byte[] utf32Bytes = string.getBytes("UTF-32"); System.out.println(utf32Bytes.length); // prints "44" final byte[] isoBytes = string.getBytes("ISO-8859-1"); System.out.println(isoBytes.length); // prints "11" final byte[] winBytes = string.getBytes("CP1252"); System.out.println(winBytes.length); // prints "11" </code></pre> <p>So you see, even a simple "ASCII" string can have different number of bytes in its representation, depending which encoding is used. Use whichever character set you're interested in for your case, as the argument to <code>getBytes()</code>. And don't fall into the trap of assuming that UTF-8 represents <em>every</em> character as a single byte, as that's not true either:</p> <pre><code>final String interesting = "\uF93D\uF936\uF949\uF942"; // Chinese ideograms // Check length, in characters System.out.println(interesting.length()); // prints "4" // Check encoded sizes final byte[] utf8Bytes = interesting.getBytes("UTF-8"); System.out.println(utf8Bytes.length); // prints "12" final byte[] utf16Bytes= interesting.getBytes("UTF-16"); System.out.println(utf16Bytes.length); // prints "10" final byte[] utf32Bytes = interesting.getBytes("UTF-32"); System.out.println(utf32Bytes.length); // prints "16" final byte[] isoBytes = interesting.getBytes("ISO-8859-1"); System.out.println(isoBytes.length); // prints "4" (probably encoded "????") final byte[] winBytes = interesting.getBytes("CP1252"); System.out.println(winBytes.length); // prints "4" (probably encoded "????") </code></pre> <p>(Note that if you don't provide a character set argument, the platform's <em>default character set</em> is used. This might be useful in some contexts, but in general you should avoid depending on defaults, and always use an explicit character set when encoding/decoding is required.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload