Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by sciSpider Java v3.0

...

If it is desired to keep only one copy of the string in memory, perform quick repeated comparisons and ensure that string1.equals(string2) is true, the following Compliant Solution may be utilizedused.

Code Block
bgColor#ccccff
public class GoodComparison {
  public static void main(String[] args) {
    String one = new String("one");
    String two = new String("one");
    boolean result;
    if (one != null){
    	one = one.intern();
    }
    if (two != null){
    	two = two.intern();
    }
    result = one == two;

   System.out.println(result);
  }
}

...

Wiki Markup
In general, for any two objects, it is permissible to compare their elements provided that the class is a singleton. The use of static factory methods over constructors facilitates instance control which in turn limits the effective number of instances of an immutable class to one. As a Thusresult, for two objects a and b, a.equals(b) is true only when a==b \[[Bloch 08|AA. Java References#Bloch 08]\]. The {{String}} class does not possess these characteristics.

...