You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中String类构造方法初始化疑问:original如何转为字符数组

Understanding String's Copy Constructor & Your Compilation Error

Hey there! Let's unpack this step by step, since it's a common point of confusion with Java's String class.

First: What's Going On in That Constructor?

The code you're looking at is String's copy constructor:

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

Here's the key context you're missing:

  • Internally, Java's String class stores its characters in a private array (called value; in JDK 9+, this is a byte array instead of char for efficiency, but the logic stays the same).
  • String is designed to be immutable — once a String object is created, its content can never be changed. Because of this, there's no risk in reusing the same value array across String instances: no external code can modify the array (it's private, and String doesn't expose any methods to alter it directly).

So this constructor isn't "converting" original to a char array — it's just reusing the existing array that original already holds. It's an optimization to avoid unnecessary array copies, since immutability guarantees safety.

Why Your Code Throws a Compilation Error

When you tried to replicate this logic, you probably wrote something like:

public class MyClass {
    public MyClass(String original) {
        char[] myValue = original.value; // Compile error here!
    }
}

The error happens because value is a private member of the String class. Java's encapsulation rules prevent any external code (including your own classes) from accessing private fields of another class directly. The String class's own constructor can access original.value only because it's part of the same class.

How to Replicate This Logic (Safely) in Your Own Code

If you want to create a copy constructor for your own immutable class, here's how to do it correctly:

import java.util.Arrays;

public class ImmutableText {
    // Private, final array to store content
    private final char[] content;
    private int hash;

    // Constructor from a char array (copies the array to enforce immutability)
    public ImmutableText(char[] content) {
        this.content = Arrays.copyOf(content, content.length);
    }

    // Copy constructor (can access private members since it's same class)
    public ImmutableText(ImmutableText original) {
        this.content = original.content; // Safe to reuse, since class is immutable
        this.hash = original.hash;
    }

    // Only expose read-only methods
    public char getCharAt(int index) {
        return content[index];
    }
}
  • For external input (like a char array), we copy the array to make sure no outside code can modify our internal state.
  • For the copy constructor, since we're dealing with another instance of our own class, we can safely reuse the internal array — just like String does — because our class's immutability ensures the array won't be altered.

If you need to create a copy of a String's content in your own code, use the public toCharArray() method (note this returns a copy of the internal array, not the original):

char[] stringContent = myString.toCharArray();

内容的提问来源于stack exchange,提问作者javaAndBeyond

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:28:31