简体   繁体   English

Java String 构造函数是如何实现的?

[英]How is Java String constructor implemented?

I was going through Java String source code, found a CTOR where I have some doubts:我正在浏览 Java String 源代码,找到了一个我有一些疑问的 CTOR:

public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}

I understood that Original is acting as literal String (with double quotes) but not able to understand how is java/jvm computed original.value as char Array.我知道 Original 充当文字String (带双引号),但无法理解 java/jvm 如何将original.value计算为char Array。 What is "value" over here??这里的“价值”是什么? if value is char Array then how .value function/Field is calculated???如果值是char数组,那么.value函数/字段是如何计算的???

Yes, as already mentioned in the comments, this is very simple.是的,正如评论中已经提到的,这非常简单。 Since you're looking at the String class itself — it has access to its own fields.由于您正在查看String类本身——它可以访问自己的字段。 And that is where the characters this given string consists of are actually stored — in a char array.这就是这个给定字符串所包含的字符实际存储的地方——在一个char数组中。 This method simply references the field by name, very basic interaction.这个方法只是简单地按名称引用字段,非常基本的交互。

The docs says 文档

Initializes a newly created String object so that it represents the same sequence of characters as the argument;初始化一个新创建的 String 对象,使其表示与参数相同的字符序列; in other words, the newly created string is a copy of the argument string.换句话说,新创建的字符串是参数字符串的副本。 Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.除非需要原始的显式副本,否则不需要使用此构造函数,因为字符串是不可变的。

Technically the new String will get the value and hash of the original .从技术上讲,新的字符串将获得valuehash的的original

在此处输入图片说明

which means that this is a copy of another String.这意味着这是另一个字符串的副本。

String by design holds Unicode text, so all language scripts may be combined.字符串按设计保存 Unicode 文本,因此可以组合所有语言脚本。 For that the implementation holds an array (field name value ), where every char is a two byte UTF-16 value.为此,实现包含一个数组(字段名称value ),其中每个字符都是一个两字节的 UTF-16 值。

You encountered the one and AFAIK only silly point in the Java classes.您在 Java 类中遇到了 AFAIK 唯一的愚蠢点。

The shown copy constructor is senseless, as Strings are immutable objects, and they may be shared by simple assignment.所示的复制构造函数毫无意义,因为字符串是不可变的对象,它们可以通过简单的赋值来共享。 It is a fossile of C++ inheritance, maybe in association with String interning.它是 C++ 继承的化石,可能与 String 实习有关。

To make a copy is senseless.复制是没有意义的。 This holds too for the internal char array, which indeed may be assigned by reference.这也适用于内部char数组,它确实可以通过引用分配。 (Not very consequent.) (后果不是很严重。)

So the following shows inexperienced java usage:所以下面显示了没有经验的java用法:

String s = new String(t);

With the newest java versions, the value of a String might actually be a byte array in some encoding, and hence the chars are lazily provided.在最新的 Java 版本中,字符串的值实际上可能是某种编码中的字节数组,因此字符被延迟提供。


About String literals:关于字符串文字:

String literals are stored in a datastructure in a .class file called the constant pool .字符串文字存储在称为常量池的 .class 文件中的数据结构中。 Stored is it as UTF-8 bytes.存储为 UTF-8 字节。 The JVM ClassLoader ensures that the string is loaded as String. JVM ClassLoader 确保字符串作为字符串加载。

Imports of final static String constants are copied into the constant pool, and the original class may no longer appear as being imported from. final static String常量的导入被复制到常量池中,并且原始类可能不再显示为从中导入。 Holding string constant in an other class may require manually doing a clean build, as there might no longer exist a class dependency.在其他类中保存字符串常量可能需要手动进行干净构建,因为可能不再存在类依赖项。

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

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