java是否将字符串定义为空终止?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18414873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:24:49  来源:igfitidea点击:

Does java define string as null terminated?

javastringnull

提问by Dewsworld

As my title suggests, it's a theoretical question. I'd like to know that if java defines string as null terminated.

正如我的标题所暗示的,这是一个理论问题。我想知道如果 java 将字符串定义为空终止。

回答by Juned Ahsan

Java strings are not terminated with a null characters as in C or C++. Although java strings uses internally the char array but there is no terminating null in that. String class provides a method called length to know the number of characters in the string.

Java 字符串不像在 C 或 C++ 中那样以空字符终止。尽管 java 字符串在内部使用 char 数组,但其中没有终止 null。String 类提供了一个名为 length 的方法来了解字符串中的字符数。

Here is the simple code and its debugger contents:

这是简单的代码及其调试器内容:

public static void main(String[] args) {
    String s = "Juned";
    System.out.println(s);
}

Debugger screenshot:

调试器截图:

enter image description here

在此处输入图片说明

回答by Adam Rosenfield

Does it matter?

有关系吗?

If you convert a Java string into some kind of serialized format (onto disk, the network, etc.), then all that matters is the serialization format, not the JVM's internal format.

如果您将 Java 字符串转换为某种序列化格式(到磁盘、网络等),那么重要的是序列化格式,而不是 JVM 的内部格式。

If you're reading the string's data in C code via JNI, then you never read the data directly, you always use JNI functions like GetStringChars()or GetStringUTFChars(). GetStringChars()is not documented as returning null-terminated data, so you shouldn't assume that it's null-terminated—you must use GetStringLength()to determine its length. Likewise with GetStringUTFChars(), you must use GetStringUTF8Length()to determine its length in modified UTF-8 format.

如果您通过 JNI 读取 C 代码中的字符串数据,那么您永远不会直接读取数据,您总是使用 JNI 函数,如GetStringChars()GetStringUTFChars()GetStringChars()没有记录为返回以 null 结尾的数据,因此您不应假设它以 null 结尾——您必须使用它GetStringLength()来确定其长度。同样GetStringUTFChars(),您必须使用GetStringUTF8Length()确定其修改后的 UTF-8 格式的长度。

回答by Stephen C

I'd like to know that if java defines string as null terminated.

我想知道如果 java 将字符串定义为空终止。

No. A String is defined to be a fixed length sequence of charvalues. All possible charvalues (from 0 to 65535) may be used in a String. There is no "distinguished" value that means that the string ends.

不可以。字符串被定义为固定长度的char值序列。char字符串中可以使用所有可能的值(从 0 到 65535)。没有表示字符串结束的“可分辨”值。

So how they track string ending? Using length?

那么他们如何跟踪字符串结尾呢?使用长度?

Yes. A Stringobject has a private lengthfield (in all implementations I've examined ...).

是的。一个String对象有一个私有length字段(在我检查过的所有实现中......)。

If you want to understand more about how Java strings are implemented, the source code for various versions is available online. Google for "java.lang.String source".

如果您想更多地了解 Java 字符串的实现方式,可以在线获取各种版本的源代码。谷歌搜索“java.lang.String 源代码”。