我可以更改传递给我的方法的 String 对象的值吗?

发布于 2024-12-17 09:43:20 字数 469 浏览 6 评论 0原文

我发现以下问题 Java 是“按引用传递”还是“按值传递” ?

我几乎阅读了所有内容,但还不知道如果我想要 foo(-) 方法来更改我的 String,我该怎么办价值? (也许或不参考,这对我来说并不重要)。

void foo(String errorText){ 
    errorText="bla bla";
}

int main(){ 
    String error="initial"; 
    foo(error); 
    System.out.println(error);
}

我想在控制台上看到 bla bla。是否可以?

I found the following question Is Java "pass-by-reference" or "pass-by-value"?.

I read almost all of it, but could not find out yet what should I do if I want the foo(-) method, to change my String's value? (maybe or not reference too, it doesn't matter to me).

void foo(String errorText){ 
    errorText="bla bla";
}

int main(){ 
    String error="initial"; 
    foo(error); 
    System.out.println(error);
}

I want to see bla bla on the console. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

您无法更改 fooerrorText 的值,因为该方法当前已声明。即使您将 String errorText 的引用传递给 foo,Java String 也是不可变的——您无法更改它们。

但是,您可以使用StringBuffer(或StringBuilder)。这些类可以在您的 foo 方法中编辑。

public class Test {
    public static void foo(StringBuilder errorText){ 
        errorText.delete(0, errorText.length());
        errorText.append("bla bla");
    }

    public static void main(String[] args) { 
        StringBuilder error=new StringBuilder("initial");
        foo(error); 
        System.out.println(error);
    }
}

其他解决方案是使用包装类(创建一个类来保存 String 引用,并更改 foo 中的引用),或者仅返回字符串。

You can't change the value of errorText in foo as the method is currently declared. Even though you are passing a reference of the String errorText into foo, Java Strings are immutable--you can't change them.

However, you could use a StringBuffer (or StringBuilder). These classes can be edited in your foo method.

public class Test {
    public static void foo(StringBuilder errorText){ 
        errorText.delete(0, errorText.length());
        errorText.append("bla bla");
    }

    public static void main(String[] args) { 
        StringBuilder error=new StringBuilder("initial");
        foo(error); 
        System.out.println(error);
    }
}

Other solutions are to use a wrapper class (create a class to hold your String reference, and change the reference in foo), or just return the string.

爱的故事 2024-12-24 09:43:20

使用方法的返回值或创建一个包装类。

让它返回值:

String foo(String errorText){ 
    return "bla bla";
}

int main(){ 
    String error="initial"; 
    error = foo(error); 
    System.out.println(error);
}

将值包装在对象中:

class StringWrapper {
    private String string;
    public StringWrapper(String s) {
        this.string = s;
    }
    public String getString() {
        return this.string;
    }
    public void setString(String s) {
        this.string = s;
    }
}

void foo(StringWrapper errorText){ 
    errorText.setString("bla bla");
}

int main(){ 
    StringWrapper error=new StringWrapper("initial"); 
    foo(error); 
    System.out.println(error.getString());
}

Either use the return value of the method or create a wrapper class.

Have it return the value:

String foo(String errorText){ 
    return "bla bla";
}

int main(){ 
    String error="initial"; 
    error = foo(error); 
    System.out.println(error);
}

Wrap the value in an object:

class StringWrapper {
    private String string;
    public StringWrapper(String s) {
        this.string = s;
    }
    public String getString() {
        return this.string;
    }
    public void setString(String s) {
        this.string = s;
    }
}

void foo(StringWrapper errorText){ 
    errorText.setString("bla bla");
}

int main(){ 
    StringWrapper error=new StringWrapper("initial"); 
    foo(error); 
    System.out.println(error.getString());
}
小霸王臭丫头 2024-12-24 09:43:20

是的,您可以借助反射来更改此设置,但这违反规则。

void foo(String errorText) {
    try {
        final Class<String> type = String.class;
        final java.lang.reflect.Field valueField = type.getDeclaredField("value");
        valueField.setAccessible(true);
        valueField.set(errorText, "bla bla".toCharArray());
    } catch (Exception e) {
    }

}

public static void main(String[] args) {
    String error = new String("initial");
    foo(error);
    System.out.println(error);
}

Yes you can change this with help of reflections but its against rule.

void foo(String errorText) {
    try {
        final Class<String> type = String.class;
        final java.lang.reflect.Field valueField = type.getDeclaredField("value");
        valueField.setAccessible(true);
        valueField.set(errorText, "bla bla".toCharArray());
    } catch (Exception e) {
    }

}

public static void main(String[] args) {
    String error = new String("initial");
    foo(error);
    System.out.println(error);
}
深海蓝天 2024-12-24 09:43:20

字符串值是不可变的——所以一旦你得到一个值,你就只能使用它。

String values are immutable -- so once you get a value, you're stuck with it.

定格我的天空 2024-12-24 09:43:20

Java 语言对文字 String 进行了特殊处理;您的代码大致相当于:

void foo(String errorText){ // at this point, errorText refers to the original string
    errorText=new String("bla bla"); // now it refers to a new string
}

int main(){ 
    String error=new String("initial"); // error is a reference to the original string
    foo(error); // pass a *copy* of the reference
    System.out.println(error);
}

换句话说,您只是将本地引用 errorText 指向不同的 String 对象,这不会影响方法之外的任何内容。

但更一般地说,String 是不可变的;没有办法修改它们。

Literal Strings are treated specially by the Java language; your code is roughly equivalent to:

void foo(String errorText){ // at this point, errorText refers to the original string
    errorText=new String("bla bla"); // now it refers to a new string
}

int main(){ 
    String error=new String("initial"); // error is a reference to the original string
    foo(error); // pass a *copy* of the reference
    System.out.println(error);
}

In other words, you're just pointing the local reference errorText at a different String object, which affects nothing outside the method.

More generally, though, Strings are immutable; there's no way to modify them.

冷血 2024-12-24 09:43:20

您可以重新分配字符串引用:

String foo(String err) {
  return "bla blah"
}

error = foo(error);

You can reassign the String reference:

String foo(String err) {
  return "bla blah"
}

error = foo(error);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文