Java String Interning
import util.*;
import java.util.Scanner;
import java.util.Arrays;
class ArrrayRefTest {
public static void main(String[] args) {
String hello1 = "hello";
String hello2 = "hello";
String hello3 = "he" + "llo";
String hello4 = new String("hello");
String hello5 = new String("he") + "llo";
String hello6 = null;
{
String hel = "hel";
String lo = "lo";
hello6 = hel + lo;
}
System.out.println("================================================");
System.out.printf("hello1 hashCode %d\n", hello1.hashCode());
System.out.printf("hello2 hashCode %d\n", hello2.hashCode());
System.out.printf("hello3 hashCode %d\n", hello3.hashCode());
System.out.printf("hello4 hashCode %d\n", hello4.hashCode());
System.out.printf("hello5 hashCode %d\n", hello5.hashCode());
System.out.printf("hello6 hashCode %d\n", hello6.hashCode());
System.out.println("================================================");
System.out.printf("hello1 identity hash %d\n", System.identityHashCode(hello1));
System.out.printf("hello2 identity hash %d\n", System.identityHashCode(hello2));
System.out.printf("hello3 identity hash %d\n", System.identityHashCode(hello3));
System.out.printf("hello4 identity hash %d\n", System.identityHashCode(hello4));
System.out.printf("hello5 identity hash %d\n", System.identityHashCode(hello5));
System.out.printf("hello6 identity hash %d\n", System.identityHashCode(hello6));
System.out.println("================================================");
String meme = "meme";
foo(meme);
System.out.println(meme);
}
public static void foo(String s) {
s = s.replaceAll("h", "m");
}
}