javaAndBeyond
javaAndBeyond

Reputation: 540

How String class in initiated with constructor java

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

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.

Upvotes: 0

Views: 71

Answers (1)

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

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.

Upvotes: 2

Related Questions