Normally you compare strings with the equals() method. However, you can compare strings with the == operator. The intern() method discards duplicate String objects, which frees up some memory. You really would only do this if your program generates a lot of duplicate strings, as it is usually faster than calling the equals() method. Here’s how you do it:
String string1 = "here is ";
String string2 = "a string";
string1 += string2; // Make string1 and string2 reference strings that are identical
string1 == string2; // This returns false right now
string1.equals(string2); // This returns true but requires the method call every time you want to compare
string1 = string1.intern();
string1 == string3; // Now this returns true
Note that only strings containing variables need to be interned. For example, if you create another string String string3 = "here is ";, it would reference the same object as string1 and you would not need to use the intern() method in order to compare using the == operator.