在Java中,如果我有一个字符串x,我如何计算该字符串中的字节数?
根据如何在Java中转换字符串和UTF8字节数组:
String s = "some text here";
byte[] b = s.getBytes("UTF-8");
System.out.println(b.length);
字符串是一个字符列表(即代码点)。用于表示字符串的字节数完全取决于使用哪种编码将其转换为字节。
也就是说,你可以把字符串转换成字节数组,然后查看它的大小,如下所示:
// 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"
因此,您可以看到,即使是一个简单的“ASCII”字符串也可以在其表示中具有不同的字节数,这取决于使用的编码。使用您感兴趣的字符集作为getBytes()的参数。不要陷入假设UTF-8将每个字符表示为一个字节的陷阱,因为这也不是真的:
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 "????")
(注意,如果不提供字符集参数,则使用平台的默认字符集。这在某些情况下可能是有用的,但一般情况下,你应该避免依赖默认值,当需要编码/解码时总是使用显式字符集。)
String实例在内存中分配一定数量的字节。也许您正在查看类似sizeof(“Hello World”)的东西,它将返回数据结构本身分配的字节数。
In Java, there's usually no need for a sizeof function, because we never allocate memory to store a data structure. We can have a look at the String.java file for a rough estimation, and we see some 'int', some references and a char[]. The Java language specification defines, that a char ranges from 0 to 65535, so two bytes are sufficient to keep a single char in memory. But a JVM does not have to store one char in 2 bytes, it only has to guarantee, that the implementation of char can hold values of the defines range.
sizeof在Java中没有任何意义。但是,假设我们有一个大的String并且一个char分配两个字节,那么String对象的内存占用至少是2 * str.length()字节。
迂腐的答案(虽然不一定是最有用的答案,这取决于你想对结果做什么)是:
string.length() * 2
Java字符串物理存储在UTF-16BE编码中,每个代码单元使用2个字节,而String.length()以UTF-16代码单元测量长度,因此这相当于:
final byte[] utf16Bytes= string.getBytes("UTF-16BE");
System.out.println(utf16Bytes.length);
这将告诉你内部char数组的大小,单位是字节。
注意:“UTF-16”将给出与“UTF-16BE”不同的结果,因为前者编码将插入一个BOM,将数组长度增加2个字节。
如果你运行64位引用:
sizeof(string) =
8 + // object header used by the VM
8 + // 64-bit reference to char array (value)
8 + string.length() * 2 + // character array itself (object header + 16-bit chars)
4 + // offset integer
4 + // count integer
4 + // cached hash code
换句话说:
sizeof(string) = 36 + string.length() * 2
对于32位虚拟机或64位虚拟机,如果有压缩OOPs (-XX:+UseCompressedOops),则引用为4字节。所以总数是:
sizeof(string) = 32 + string.length() * 2
这没有考虑到对字符串对象的引用。
要避免try catch,请使用:
String s = "some text here";
byte[] b = s.getBytes(StandardCharsets.UTF_8);
System.out.println(b.length);
尝试使用apache commons:
String src = "Hello"; //This will work with any serialisable object
System.out.println(
"Object Size:" + SerializationUtils.serialize((Serializable) src).length)
推荐文章
- 如何在PHP中截断字符串最接近于一定数量的字符?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap
- web - inf在Java EE web应用程序中用于什么?
- Java 8: Lambda-Streams,过滤方法与异常
- 将JsonNode转换为POJO
- 如何查看IntelliJ中的编译错误列表?
- Java SimpleDateFormat("yyyy-MM-dd' t ' hh:mm:ss' z '")给出的时区为IST
- 杰克逊克服下划线,支持骆驼案
- List、List<?>, List<T>, List<E>, List<Object>
- 在Java中转换字符串到日历对象