Skip to content

Instantly share code, notes, and snippets.

@Karasiq
Created November 8, 2014 20:43
Show Gist options
  • Save Karasiq/12e81a61b162adb25dc3 to your computer and use it in GitHub Desktop.
Save Karasiq/12e81a61b162adb25dc3 to your computer and use it in GitHub Desktop.
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void swap(Object a, Object b) {
try {
java.lang.reflect.Field value = a.getClass().getDeclaredField("value");
value.setAccessible(true);
Object tmp = value.get(a);
value.set(a, value.get(b));
value.set(b, tmp);
} catch (Exception e) {}
}
public static void main(String[] args) {
String first = "first";
String second = "second";
System.out.println(first+" "+second);
swap(first, second);
System.out.println(first+" "+second);
System.out.println();
Integer x = 1;
Integer y = 2;
System.out.println(x+" "+y);
swap(x, y);
System.out.println(x+" "+y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment