Java’s String.length() and String.getBytes().length – Java 的 String.length() 和 String.getBytes().length

最后修改: 2024年 1月 3日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

When we work in Java, manipulating strings is one of the fundamental skills. So, understanding string-related methods is crucial for writing efficient and error-free code.

当我们使用 Java 时,操作字符串是基本技能之一。因此,了解与字符串相关的方法对于编写高效、无差错的代码至关重要。

Two commonly used methods, String.length() and String.getBytes().length, may seem similar at first glance, but they serve distinct purposes.

两个常用的方法,String.length()String.getBytes().length, 乍一看似乎很相似,但它们的作用却截然不同。

In this tutorial, let’s understand these two methods and explore their differences. In addition, we’ll talk about when to use each one.

在本教程中,我们将了解这两种方法,并探讨它们之间的区别。此外,我们还将讨论何时使用这两种方法。

2. First Glance at String.length() and String.getBytes().length

2.初识 String.length()String.getBytes().length

As the method name implies, the String.length() method returns the length of a string. On the other side, String.getBytes() gets the byte array with the default encoding from the given string. Then, String.getBytes().length reports the array’s length.

正如方法名称所示,String.length() 方法返回字符串的长度。另一方面,String.getBytes() 从给定的字符串中获取默认编码的字节数组。然后,String.getBytes().length 将报告数组的长度。

If we write a test, we may see they return the same value:

如果我们进行测试,就会发现它们返回的值是一样的:

String s = "beautiful";
assertEquals(9, s.length());
assertEquals(9, s.getBytes().length);

When dealing with a string in Java, is it guaranteed that String.length() and String.getBytes().length always yield the same value?

在 Java 中处理字符串时,是否可以保证 String.length()String.getBytes().length 始终产生相同的值?

Next, let’s figure it out.

接下来,让我们来想想办法。

3. String.length() and String.getBytes().length Can Return Different Values

3.String.length()String.getBytes().length会返回不同的值

The default character encoding or charset of the current JVM plays an important role in deciding the result of String.getBytes().length. If we don’t pass any argument to String.getBytes(), it uses the default encoding scheme to encode.

当前 JVM 的默认字符编码或字符集在决定 String.getBytes().length 的结果方面起着重要作用。如果我们不向 String.getBytes() 传递任何参数,它将使用默认编码方案进行编码。

We can check the default encoding of a Java environment using the Charset.defaultCharset().displayName() method. For example, the current JVM’s default encoding is UTF-8:

我们可以使用 Charset.defaultCharset().displayName() 方法检查 Java 环境的默认编码。例如,当前 JVM 的默认编码是 UTF-8

System.out.println(Charset.defaultCharset().displayName());
//output: UTF-8

So, next, let’s test two more strings to see if String.length() and String.getBytes().length still return the same value:

接下来,让我们再测试两个字符串,看看 String.length()String.getBytes().length 是否仍然返回相同的值:

String de = "schöne";
assertEquals(6, de.length());
assertEquals(7, de.getBytes().length);

String cn = "美丽";
assertEquals(2, cn.length());
assertEquals(6, cn.getBytes().length);

As the test above shows, first, we tested with the word “beautiful” in German (“schöne”), and then we took another string, which was “beautiful” in Chinese (“美丽”). It turned out that String.length() and String.getBytes().length yielded different values in both tests.

如上面的测试所示,首先,我们使用德语单词”beautiful“(“schöne”)进行测试,然后我们使用另一个字符串,即中文字符串”beautiful“(“美丽”)。结果发现,在两次测试中,String.length()String.getBytes().length 产生了不同的值。

Next, let’s find out why this happened.

接下来,让我们来看看为什么会出现这种情况。

4. Character Encoding

4.字符编码

Before learning why String.length() and String.getBytes().length gave different values on the strings “schöne” and “美丽”, let’s quickly understand how character encoding works.

在了解 String.length()String.getBytes().length 为何会对字符串 “schöne”“美丽”给出不同的值之前,让我们快速了解一下字符编码是如何工作的。

There are many character encoding schemes, such as UTF-8 and UTF-16. We can split these encoding schemes into two categories:

有许多字符编码方案,如 UTF-8 和 UTF-16。我们可以将这些编码方案分为两类:

  • Variable-length encoding
  • Fixed-length encoding

We won’t dive too deep into character encodings. However, a general understanding of these two encoding techniques will be pretty helpful in understanding why String.getBytes().length can have different values from String.length().

我们不会深入研究字符编码。但是,对这两种编码技术的一般了解将有助于理解为什么 String.getBytes().lengthString.length(). 的值不同。

So, next, let’s take a quick look at these two kinds of encoding kinds through examples.

接下来,让我们通过示例快速了解一下这两种编码方式。

4.1. Fixed-Length Encoding

4.1.固定长度编码

The fixed-length encoding uses the same number of bytes to encode any characters. A typical example of fixed-length encoding is UTF-32, which always uses four bytes to encode a character. So, this is how “beautiful” is encoded with UTF-32:

固定长度编码使用相同数量的字节对任何字符进行编码。固定长度编码的一个典型例子是 UTF-32,它总是使用四个字节来编码一个字符。因此,UTF-32 就是这样编码 “beautiful”的:

char    byte1 byte2 byte3 byte4
 b        0     0     0     98
 e        0     0     0     101
 a        0     0     0     97
 u        0     0     0     117
 ...
 l        0     0     0     108

Therefore, when invoking String.getBytes() with the UTF-32 charset, the length of the resulting byte array will consistently be four times the number of characters in the string:

因此,在使用UTF-32 字符集调用 String.getBytes() 时,生成的字节数组长度将始终是字符串中字符数的四倍: String.getBytes() 将在 String.getBytes() 中生成的字节数组长度将始终是字符串中字符数的四倍。

Charset UTF_32 = Charset.forName("UTF_32");

String en = "beautiful";
assertEquals(9, en.length());
assertEquals(9 * 4, en.getBytes(UTF_32).length);

String de = "schöne";
assertEquals(6, de.length());
assertEquals(6 * 4, de.getBytes(UTF_32).length);

String cn = "美丽";
assertEquals(2, cn.length());
assertEquals(2 * 4, cn.getBytes(UTF_32).length);

That is to say, if UTF-32 was set as the default encoding of JVM, the results of String.length() and String.getBytes().length are always different.

也就是说,如果UTF-32 被设置为 JVM 的默认编码String.length()String.getBytes().length 的结果总是不同的

Some of us might observe that when storing UTF-32 encoded characters, even though certain characters, such as ASCII characters, only need a single byte, we still allocate four bytes, with three of them being filled with zeros. This is kind of inefficient.

有些人可能会注意到,在存储 UTF-32 编码字符时,即使某些字符(如 ASCII 字符)只需要一个字节,我们仍然要分配四个字节,其中三个字节填充 0。这有点效率低下。

So, variable-length character encoding was introduced.

于是,可变长度字符编码被引入。

4.2. Variable-Length Encoding

4.2.变长编码

Variable-length encoding uses varying numbers of bytes to encode different characters. UTF-8 is our default encoding. Also, it’s one example of the variable-length encoding schemes. So, let’s look at how UTF-8 encodes characters.

变长编码使用不同的字节数对不同的字符进行编码。UTF-8 是我们的默认编码。同时,它也是可变长度编码方案的一个例子。那么,让我们来看看 UTF-8 是如何对字符进行编码的。

UTF-8 uses from one to four bytes to encode a character depending on the character’s code point. The code point is an integer representation of a character. For example, ‘b’ has the code point 98 in decimal or U+0062 in hex-decimal, which is the same as its ASCII code.

UTF-8根据字符的码位使用一到四个字节对字符进行编码。码位是字符的整数表示。例如,‘b’ 在十进制中的码位是 98,在十六进制中的码位是 U+0062,这与其 ASCII 码相同。

Next, let’s see how UTF-8 determines how many bytes are used for encoding a character:

接下来,让我们看看 UTF-8 是如何确定一个字符的编码字节数的:

Code point range Number of bytes
U+0000 to U+007F 1
U+0080 to U+07FF 2
U+0800 to U+FFFF 3
U+10000 to U+10FFFF 4

We know the character ‘b”s code point is U+0062, which is in the range of the first row of the table above. So, UTF-8 uses only one byte to encode it. As U+0000 to U+007F is 0 to 127 in decimal, UTF-8 utilizes one single byte to encode all standard ASCII characters. That’s why String.length() and String.getBytes().length gave the same result (9) on the string “beautiful“.

我们知道字符 “b “的码位是 U+0062,在上表第一行的范围内。因此,UTF-8 只用一个字节对其进行编码。由于 U+0000 至 U+007F 是十进制的 0 至 127,UTF-8 只用一个字节来编码所有标准 ASCII 字符。这就是为什么 String.length()String.getBytes().length 在字符串”beautiful“上给出了相同的结果(9)。

However, if we check the code points of ‘ö’, ‘美’, and ‘丽’, we’ll see UTF-8 uses different numbers of bytes to encode them:

但是,如果我们检查 “ö”、”美 “和 “丽 “的码位,就会发现 UTF-8 使用不同的字节数对它们进行编码:

assertEquals("f6", Integer.toHexString('ö'));   // U+00F6 -> 2 bytes
assertEquals("7f8e", Integer.toHexString('美')); // U+7F8E -> 3 bytes
assertEquals("4e3d", Integer.toHexString('丽')); // U+4E3D -> 3 bytes

Therefore, “schöne”.getBytes().length returns 7 (5 + 2) and “美丽”.getBytes().length yields 6 (3 + 3).

因此,“schöne”.getBytes().length 返回 7 (5 + 2),“美丽”.getBytes().length 返回 6 (3 + 3)。

5. How to Choose Between String.length() and String.getBytes().length

5.如何在 String.length()String.getBytes().length 之间做出选择

Now, we have clarity on the scenarios where String.length() and String.getBytes().length return identical values and when they diverge. Then, a question may come up: when should we opt for each method?

现在,我们已经明确了 String.length()String.getBytes().length 返回相同值的情况,以及它们出现分歧的情况。然后,可能会出现一个问题:我们应该在什么时候选择使用这两种方法?

When deciding between these methods, we should consider the context of our task:

在选择这些方法时,我们应该考虑任务的背景:

  • String.length() – When we work with characters and the logical content of the string and want to obtain the total number of characters in the string, such as user input max-length validation or shifting characters in a string
  • String.bytes().length – When we deal with byte-oriented operations and need to know the size of the string in terms of bytes, such as reading from or writing to files or network streams

It’s worth noting when we work with String.bytes(), we should remember that character encoding plays a significant role. String.bytes() uses the default encoding scheme to encode the string. Apart from that, we can also pass the desired charset to the method to encode the string, for example, String.bytes(Charset.forName(“UTF_32”)) or String.bytes(StandardCharsets.UTF_16) 

值得注意的是,当我们使用 String.bytes() 时,我们应该记住,字符编码起着重要作用String.bytes() 使用默认编码方案对字符串进行编码。除此之外,我们还可以向该方法传递所需的字符集来对字符串进行编码,例如,String.bytes(Charset.forName(“UTF_32”))String.bytes(StandardCharsets.UTF_16) </em

6. Conclusion

6.结论

In this article, we understood in general how character encoding works and explored why String.length() and String.getBytes().length can produce different results. In addition, we discussed how to choose between String.length() and String.getBytes().length.

在本文中,我们大致了解了字符编码的工作原理,并探讨了 String.length()String.getBytes().length 为什么会产生不同的结果。此外,我们还讨论了如何在 String.length()String.getBytes().length 之间做出选择。

As always, the complete source code for the examples is available over on GitHub.

与往常一样,这些示例的完整源代码可在 GitHub 上获取。