かなりすごいブログ

Javaにおいて “Hoge” は単なる new String(“Hoge”) のシンタックスシュガーではない

Java4年目にしてちゃんと検証した。

class KanariSugoiClass {
  public static void main(String[] args) {
    String a,b,c,d,e;
    a = "hoge";
    b = "hoge";
    c = new String("hoge");
    d = new String("hoge");
    e = new String(a);
    System.out.print(
      "a and b are " + (a == b ? "same" : "not same") + " objects.\n" +
      "a and c are " + (a == c ? "same" : "not same") + " objects.\n" +
      "c and d are " + (c == d ? "same" : "not same") + " objects.\n" +
      "a and e are " + (a == e ? "same" : "not same") + " objects.n"
    );
  }
}
/*
  a and b are same objects.
  a and c are not same objects.
  c and d are not same objects.
  a and e are not same objects.
*/

よし。