简体   繁体   English

将String对象作为参数传递Java

[英]Passing String objects as arguments Java

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 s1 object as reference in Line 1. 我认为在Java中,对象参数作为引用传递,如果是这种情况,那么我将String s1对象作为引用传递给第1行。

And s1 should have been set to "this is a test" via display() method.. Right ? 并且应该通过display()方法将s1设置为“this is a test”。对吗?

Java is pass-by-value always. Java总是以值传递。 For reference types, it passes a copy of the value of the reference. 对于引用类型,它传递引用值的副本。

In your 在你的

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

The String s reference is reassigned, its value is changed. 重新分配String s引用,其值已更改。 The called code won't see this change because the value was a copy. 被调用的代码不会看到此更改,因为该值是副本。

With String s, it's hard to show behavior because they are immutable but take for example 使用String ,很难显示行为,因为它们是不可变的,但以此为例

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 new reference. 原始文件仍会显示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. 当然,您始终可以返回新引用并将其分配给某个变量,甚至可能是您传递给方法的变量。

String is a reference which is passed by value. String是一个按值传递的引用。 You can change where the reference points but not the caller's copy. 您可以更改参考点的位置,但不能更改调用者的副本。 If the object were mutable you could change it's content. 如果对象是可变的,你可以改变它的内容。

In short, Java ALWAYS passed by VALUE, it never did anything else. 简而言之,Java总是通过VALUE传递,它从未做过任何其他事情。

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. 正如其他人所提到的,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))

Actually in the program you are having two reference string variable s1 and s when you are calling display(s1). 实际上在程序中,当你调用display(s1)时,你有两个引用字符串变量s1和s。 Both s1 and s will be referencing to String "another". s1和s都将引用String“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" 显示(s1) - >其中包含s的引用,将打印“这是一个测试”

Only if you assign s= display(s1) both variable will refer to same string 仅当您指定s = display(s1)时,两个变量都将引用相同的字符串

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);然后string 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 value of the swap function to 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;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM