I was asked to implement a swap method in java. I had no idea what that even was. So the interviewer gave me the below. And I totally bombed it with my implementation. could someone explain me the details of how and why this should be solved?
public class swapper {
public static void main(String[] args) {
String s1 = "first";
String s2 = "second";
System.out.println("First: " + s1);
System.out.println("Second: " + s2);
swap(s1, s2);
System.out.println("First: " + s1);
System.out.println("Second: " + s2);
}
public static void swap(String s1, String s2) {
String[] myarr = new String[2];
myarr[0] = s1;
myarr[1] = s2;
s2 = myarr[0];
s1 = myarr[1];
}
}