
import java.util.*;
import java.lang.reflect.*;

public class Hash {

  public static void main(String[] args) throws Exception {
    
    int hash = args[0].hashCode();

    String copy = new String(args[0]);
    byte[] bytes = copy.getBytes();

    System.out.println("hash " + args[0] + " " + hash);

    Map map1 = new HashMap();

    Map map2 = new TreeMap();

    map1.put(args[0],args[0]);

    map2.put(args[0],args[0]);

    System.out.println("get hashMap " + map1.get(args[0]));

    System.out.println("get treeMap " + map2.get(args[0]));

    changeString(args[0]);

    System.out.println("---------------------------------------------");
    System.out.println("hash " + args[0] + " " + (args[0].hashCode()));
    System.out.println("get hashMap " + map1.get(args[0]));
    System.out.println("get treeMap " + map2.get(args[0]));

    System.out.println("copy = " + copy);
    
    copy = new String(bytes);

    System.out.println("---------------------------------------------");
    System.out.println("COPY from bytes: hash " + copy + " " + (copy.hashCode()));
    System.out.println("get hashMap " + map1.get(copy));
    System.out.println("get treeMap " + map2.get(copy));

  }

  // NEVER DO THIS !!!
  public static void changeString(String string) throws Exception {

    Class stringClass = string.getClass();
    Field[] f = stringClass.getDeclaredFields();
    System.out.println(Arrays.toString(f));
    Field f1 = f[0];
    f1.setAccessible(true);
    char[] value = (char[]) f1.get(string);
    System.out.println("value of field0 in " + string + " is " + value);
    System.out.println("or " + Arrays.toString(value));
    value[0] = 'x';
    System.out.println("value of field0 in " + string + " is " + value);
    System.out.println("or " + Arrays.toString(value));
  }
}

/***************************************************************************

>java Hash test
hash test 3556498
get hashMap test
get treeMap test
[private final char[] java.lang.String.value, private final int java.lang.String.offset, private final int java.lang.String.count, private int java.lang.String.hash, private static final long java.lang.String.serialVersionUID, private static final java.io.ObjectStreamField[] java.lang.String.serialPersistentFields, public static final java.util.Comparator java.lang.String.CASE_INSENSITIVE_ORDER]
value of field0 in test is [C@1eed786
or [t, e, s, t]
value of field0 in xest is [C@1eed786
or [x, e, s, t]
---------------------------------------------
hash xest 3556498
get hashMap xest
get treeMap xest
copy = xest
---------------------------------------------
COPY from bytes: hash test 3556498
get hashMap null
get treeMap null

****************************************************************************/
