How String class in initiated with constructor java

前端 未结 1 1849
情书的邮戳
情书的邮戳 2021-01-27 10:37
  public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}    

I dont understand how original is being conver

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-27 10:53

    String has two properties:

    /** The value is used for character storage. */
    private final char value[];
    
    /** Cache the hash code for the string */
    private int hash; // Default to 0
    

    Because you're inside the constructor of String, you have access right to its private field value[] and hash.

    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.

    0 讨论(0)
提交回复
热议问题