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


我不明白原始是如何转换为char数组的。如果我尝试使用其他代码,则会引发编译错误。

最佳答案

String具有两个属性:

/** The value is used for character storage. */
private final char value[];

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


因为您位于String的构造函数中,所以您有权访问其专用字段value[]hash

您无法从String类外部访问这些private字段,因此,如果尝试执行此操作,则会引发编译错误。

10-05 21:12
查看更多