如何使用构造函数java启动String类 [英] How String class in initiated with constructor java

查看:135
本文介绍了如何使用构造函数java启动String类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我不明白原始文件是如何转换为char数组的。如果我用不同的代码尝试它,它会抛出一个编译错误。

I dont understand how original is being converted to a char array. If i try it with a different code, its throwing a compilation error.

推荐答案

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 <的构造函数中/ code>,您可以访问其私有字段 value [] hash

您无法从String类外部访问那些私有字段,因此如果您尝试访问它将引发编译错误做到这一点。

You cann't access those private fields from outside of String class, hence it will throw a compilation error if you attempt to do it.

这篇关于如何使用构造函数java启动String类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆