fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.lang.reflect.Field;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone {
  10.  
  11. public static void main(String[] args) throws Exception {
  12. final String hello = "Hello World!";
  13. describe(hello);
  14. mutateString(hello, "Goodbye World!");
  15. describe(hello);
  16. }
  17.  
  18. private static void mutateString(String object, String newContent) throws Exception {
  19. final Field f = String.class.getDeclaredField("value");
  20. f.setAccessible(true);
  21. f.set(object, newContent.toCharArray());
  22. }
  23.  
  24. private static void describe(String object) {
  25. System.out.println(System.identityHashCode(object) + " " + object);
  26. }
  27. }
Success #stdin #stdout 0.08s 27796KB
stdin
Standard input is empty
stdout
356573597 Hello World!
356573597 Goodbye World!