Reputation: 3311
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 ?
Upvotes: 8
Views: 48128
Reputation: 279910
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.
Upvotes: 20
Reputation: 85
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;
}
}
Upvotes: 0
Reputation: 5521
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
Upvotes: 0
Reputation: 68715
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))
Upvotes: 2
Reputation: 533492
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.
Upvotes: 3