简体   繁体   中英

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" ?.

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.

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

Java is pass-by-value always. 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. 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

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.


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. 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.

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.

I believe you want to do this to assign the returned value back to string 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). Both s1 and s will be referencing to 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"

now s and s1 are holding refence to two different stings

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

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

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.

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

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;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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