Java is Strictly Pass by value.
Let's modify your code as below from the explanation point of view
static void swap(String str1, String str2){
String temp = str1;
str1=str2;
str2=temp;
}
public static void main(String[] args) {
String s1 = "Hello", s2 = "world";
swap(s1, s2);
System.out.println(s1 + s2);
}
S1 and S2 are the references which hold the address of the string object. For ex: S1=123 , S2= 234
When we call swap method, copy of S1 and S2 is created and will be assigned to str1 and str2.
Which will be str1=123 and str2=234.
Swap method will only swap the values present in str1 and str2. (str1=234 str2=123) whereas S1 and S2 still points to the same memory location.
This is the reason why the swap is not reflected in the main method.