gpt4 book ai didi

java - 为什么String类有拷贝构造函数?

转载 作者:搜寻专家 更新时间:2023-10-30 20:01:14 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
What is the purpose of the expression “new String(…)” in Java?

如果不可变类对象副本与原始对象相等,那么为什么 Java 中的 String 类有一个复制构造函数?这是一个错误还是此实现背后有原因?在 Java 文档中指定:

/**
* Initializes a newly created {@code String} object so that it represents
* the same sequence of characters as the argument; in other words, the
* newly created string is a copy of the argument string. Unless an
* explicit copy of {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code String}
*/
public String(String original) {
....
....}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com