gpt4 book ai didi

java - 从参数中将字符串内容分配给 Java 中的新字符串

转载 作者:行者123 更新时间:2023-11-29 10:05:16 26 4
gpt4 key购买 nike

有这个方法

void doSomething(String input)
{
//trick for create new object instead of reference assignment
String workStr= "" + input;

//work with workStr
}

Java 的实现方式是什么?

编辑

  • 如果我使用输入变量作为 input=something,那么 Netbeans 会发出关于赋值的警告方法参数
  • 如果我使用 new String(input) 创建它,它会警告使用 String 构造函数

也许解决方案不是不为输入分配任何内容,或者只是忽略警告..

最佳答案

String copy = new String(original);

Initializes a newly created 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 original is needed, use of this constructor is unnecessary since Strings are immutable.

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#String(java.lang.String )

关于java - 从参数中将字符串内容分配给 Java 中的新字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10537390/

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