I have studied that java is pass by reference but when I execute following code the strings are not swapped in main method why?
static void swap(String s1, String s2){
String temp = s1;
s1=s2;
s2=temp;
}
public static void main(String[] args) {
String s1 = "Hello", s2 = "world";
swap(s1, s2);
System.out.println(s1 + s2);
}
No comments:
Post a Comment