简体   繁体   English

如何在Java内部表示字符串?

[英]How is a string represented in Java internally?

我知道C字符串abc在C内部是abc\\0 ,与Java的情况是一样的吗?

No, it's not the same in Java. 不,它在Java中不一样。 There's no null terminator. 没有空终止符。 Java strings are objects, not points to arrays of characters. Java字符串是对象,而不是指向字符数组。 It maintains the length along with the Unicode characters, so there's no need to look for a null terminator. 它保持长度以及Unicode字符,因此不需要查找空终止符。

You don't have to ask here: look at the source for String.java in the src.zip that ships with your JDK. 您不必在此处询问:查看JDK附带的src.zip中String.java的源代码。 Here's the start of it: 这是它的开始:

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence
{
    /** The value is used for character storage. */
    private final char value[];

    /** The offset is the first index of the storage that is used. */
    private final int offset;

    /** The count is the number of characters in the String. */
    private final int count;

    /** Cache the hash code for the string */
    private int hash; // Default to 0

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -6849794470754667710L;
}

Nope, C strings are an array of chars and thus there is no length associated with them. 不,C字符串是一个字符数组,因此没有与它们相关的长度。 The side affect of this decision is that to determine a string's length, one must iterate through it to find the \\0 , which isn't as efficient of carrying the length around. 这个决定的副作用是确定一个字符串的长度,必须遍历它才能找到\\0 ,这对于携带长度来说效率不高。

Java strings have a char array for their chars and carry an offset length and a string length. Java字符串为其字符设置了char数组,并带有偏移长度和字符串长度。 This means determining a string's length is rather efficient. 这意味着确定字符串的长度是相当有效的。

Source . 来源

C语言中的字符串是char类型的数组,在Java中它是一个类,它表示unicode字符的集合。

No. Null terminators are used in C because it's easier than passing around a pointer and a size. 在N中使用Null终结符,因为它比绕指针和大小更容易。 In Java, the size is always known and so a null terminator isn't necessary. 在Java中,大小始终是已知的,因此不需要空终止符。 Furthermore, there are no terminating characters in Java (putting in a \\0 would be part of the literal string). 此外,Java中没有终止字符(放入\\0将是文字字符串的一部分)。

Java strings are not null-terminated like C strings. Java字符串不像C字符串那样以null结尾。 This is because Java stores strings' lengths. 这是因为Java存储了字符串的长度。 You can retrieve the length with String.length() . 您可以使用String.length()检索长度。

Class String is implemented in Java. Class String以Java实现。 See OpenJDK's implementation for an example. 有关示例,请参阅OpenJDK的实现

The OpenJDK 7 String class carries an array of type char[] to hold the string itself, as well as an offset (telling the first used position in the char[] ), the length of the string, and the hash code of the string. OpenJDK 7 String类携带一个char[]类型的数组来保存字符串本身,以及一个偏移量(告诉char[]的第一个使用位置),字符串的长度以及字符串的哈希码。

It also has two static fields, a version ID for serialization purposes and an ObjectStreamField[] due to special casing with respect to the serialization output stream (in OpenJDK 7 at least). 它还有两个静态字段,一个用于序列化目的的版本ID和一个ObjectStreamField[]因为它有关于序列化输出流的特殊外壳(至少在OpenJDK 7中)。

As far as I know, in java String is stored as an object in the heap section as a sub-class of Object. 据我所知,在java中,String作为Object的子类存储为堆的一个对象。 There is no need to use '\\0' to specify just characters or String. 不需要使用'\\ 0'来指定字符或字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM