Java always passes the references by value. Read this answer for more details. However, you can swap two strings via arrays or wrapper classes.
EDIT:
@dasblinkenlight shows how to use arrays to swap two strings, and here is an example of using wrapper class:
public class WrapperString
{
public String text;
public WrapperString(String text){this.text = text}
@Override
public String toString(){return text;}
}
public static void swap(WrapperString a, WrapperString b)
{
String temp = a.text;
a.text = b.text;
b.text = temp;
}
public static void main(String[] args)
{
WrapperString s1 = new WrapperString("Hello");
WrapperString s2 = new WrapperString("World");
swap(s1, s2);
System.out.println(s1.text + s2.text);
// or System.out.println(s1 + s2); since we've already overridden toString()
}
OUTPUT:
WorldHello