将字符串对象作为参数传递 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18662253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:57:39  来源:igfitidea点击:

Passing String objects as arguments Java

java

提问by UnderDog

class prog {

      static String display(String s)
      {
         s = "this is a test";
         return s;
      }

   public static void main(String...args) {

     prog p = new prog();
     String s1 = "another";
     System.out.println(display(s1)); //Line 1
     System.out.println(s1);
   }
}

A newbie question.

一个新手问题。

Can someone explain why s1 is not getting updated to "this is a test" ?.

有人可以解释为什么 s1 没有更新为“这是一个测试”?

I thought in Java, object arguments are passed as references and if that is the case, then I am passing String s1object as reference in Line 1.

我认为在 Java 中,对象参数作为引用传递,如果是这种情况,那么我s1在第 1 行中将String对象作为引用传递。

And s1should have been set to "this is a test" via display()method.. Right ?

并且s1应该通过display()方法设置为“这是一个测试” 。对吗?

采纳答案by Sotirios Delimanolis

Java is pass-by-value always. For reference types, it passes a copy of the value of the reference.

Java 总是按值传递。对于引用类型,它传递引用值的副本。

In your

在你的

static String display(String s)
{
    s = "this is a test";
    return s;    
}

The String sreference is reassigned, its value is changed. The called code won't see this change because the value was a copy.

String s参考被重新分配,则其值被改变。被调用的代码不会看到此更改,因为该值是一个副本。

With Strings, it's hard to show behavior because they are immutable but take for example

使用Strings,很难显示行为,因为它们是不可变的,但举个例子

public class Foo {
    int foo;
}

public static void main(String[] args) {
    Foo f = new Foo();
    f.foo = 3;
    doFoo(f);
    System.out.println(f.foo); // prints 19
}

public static void doFoo(Foo some) {
    some.foo = 19;
}

However, if you had

然而,如果你有

public static void doFoo(Foo some) {
    some = new Foo();
    some.foo = 19;
}

the original would still show 3, because you aren't accessing the object through the reference you passed, you are accessing it through the newreference.

原始文件仍会显示3,因为您不是通过传递的引用访问对象,而是通过new引用访问它。



Of course you can always return the new reference and assign it to some variable, perhaps even the same one you passed to the method.

当然,您始终可以返回新引用并将其分配给某个变量,甚至可能是您传递给方法的同一个变量。

回答by Peter Lawrey

Stringis a reference which is passed by value. You can change where the reference points but not the caller's copy. If the object were mutable you could change it's content.

String是按值传递的引用。您可以更改参考点的位置,但不能更改调用者的副本。如果对象是可变的,你可以改变它的内容。

In short, Java ALWAYS passed by VALUE, it never did anything else.

简而言之,Java 总是通过 VALUE,它从不做任何其他事情。

回答by Juned Ahsan

As others mentioned String s1 is a reference which is passed by value, and hence s1 reference in your method still points to the old string.

正如其他人提到的,字符串 s1 是按值传递的引用,因此您方法中的 s1 引用仍然指向旧字符串。

I believe you want to do this to assign the returned value back to string s1:

我相信您想这样做以将返回值分配回字符串 s1:

String s1 = "another";
s1 = display(s1);

System.out.println(display(s1))

回答by upog

Actually in the program you are having two reference string variable s1 and s when you are calling display(s1). Both s1 and s will be referencing to String "another".

实际上,在程序中,当您调用 display(s1) 时,您有两个参考字符串变量 s1 和 s。s1 和 s 都将引用字符串“another”。

but inside the display method you are changing the reference of s to point another String "this is a test" and s1 will still point to "another"

但是在显示方法中,您将 s 的引用更改为指向另一个字符串“这是一个测试”,而 s1 仍将指向“另一个”

now s and s1 are holding refence to two different stings

现在 s 和 s1 正在对两个不同的刺进行引用

display(s1) --> which hold reference of s, will print "this is a test"

display(s1) --> 保存 s 的引用,将打印“这是一个测试”

Only if you assign s= display(s1) both variable will refer to same string

仅当您分配 s= display(s1) 时,两个变量才会引用相同的字符串

回答by Shivam Shukla

Because String is immutable so changes will not occur if you will not assign the returned value of function to the string.so in your question assign value of display(s1) to s.

因为 String 是不可变的,所以如果您不将函数的返回值分配给 string.so 在您的问题中将 display(s1) 的值分配给 s,则不会发生更改。

s=display(s1);then the value of string s will change.

s=display(s1);那么字符串s的值就会改变。

I was also getting the unchanged value when i was writing the program to get some permutations string(Although it is not giving all the permutations but this is for example to answer your question)

当我编写程序以获取一些排列字符串时,我也得到了不变的值(虽然它没有给出所有排列,但这是例如回答你的问题)

Here is a example.

这是一个例子。

import java.io.*;
public class MyString {
public static void main(String []args)throws IOException{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String s=br.readLine().trim();
    int n=0;int k=0;
    while(n!=s.length()){
        while(k<n){
        swap(s,k,n);
        System.out.println(s);
        swap(s,k,n);
        k++;
        }
        n++;
    }


}
public static void swap(String s,int n1,int n2){
    char temp;
    temp=s.charAt(n1);
    StringBuilder sb=new StringBuilder(s);
    sb.setCharAt(n1,s.charAt(n2));
    sb.setCharAt(n2,temp);
    s=sb.toString();
}
}

but i was not getting the permuted values of the string from above code.So I assigned the returned valueof the swap functionto the string and got changed values of string. after assigning the returned value i got the permuted values of string.

但我没有从上面的代码中获得字符串的置换值。所以我将交换函数返回值分配给字符串并获得了字符串的更改值。分配返回值后,我得到了字符串的排列值。

//import java.util.*;
import java.io.*;
public class MyString {
public static void main(String []args)throws IOException{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String s=br.readLine().trim();
    int n=0;int k=0;
    while(n!=s.length()){
        while(k<n){
        s=swap(s,k,n);
        System.out.println(s);
        s=swap(s,k,n);
        k++;
        }
        n++;
    }


}
public static String swap(String s,int n1,int n2){
    char temp;
    temp=s.charAt(n1);
    StringBuilder sb=new StringBuilder(s);
    sb.setCharAt(n1,s.charAt(n2));
    sb.setCharAt(n2,temp);
    s=sb.toString();
    return s;
}
}