String Constructors

The String class has numerous constructors to create and initialize String objects based on various types of arguments. Here we present a few selected constructors:

String()

Creates a new String object, whose content is the empty string, “”.

String(String str)

Creates a new String object, whose contents are the same as those of the String object passed as the argument.

Click here to view code image

String(char[] value)
String(char[] value, int offset, int count)

Create a new String object, whose contents are copied from a char array. The second constructor allows extraction of a certain number of characters (count) from a given offset in the specified array.

Click here to view code image

String(StringBuilder builder)

This constructor allows interoperability with the StringBuilder class.

Note that using a constructor creates a brand-new String object; using a constructor does not intern the string. In the following code, the String object denoted by str4 is different from the interned String object passed as an argument:

Click here to view code image

String str4 = new String(“You cannot change me!”);

Constructing String objects can also be done from arrays of bytes, arrays of characters, and string builders:

Click here to view code image

byte[] bytes = {97, 98, 98, 97};
char[] characters = {‘a’, ‘b’, ‘b’, ‘a’};
StringBuilder strBuilder = new StringBuilder(“abba”);
//…
String byteStr  = new String(bytes);      // Using array of bytes: “abba”
String charStr  = new String(characters); // Using array of chars: “abba”
String buildStr = new String(strBuilder); // Using string builder: “abba”

In Example 8.3, note that the reference str1 does not denote the same String object as the references str4 and str5. Using the new operator with a String constructor always creates a new String object. The expression “You cannot” + words is not a constant expression, and therefore, results in the creation of a new String object. The local references str2 and str3 in the main() method and the static reference str1 in the Auxiliary class all denote the same interned string. Object value equality is hardly surprising between these references.

The String method intern() allows the contents of a String object to be interned. If a string with the same contents is not already in the string pool, it is added; otherwise, the already interned string is returned. The following relationship holds between any two strings strX and strY:

Click here to view code image

strX.intern() == strY.intern()
is
 true
if and only if
 strX.equals(strY)
is
 true.

In Example 8.3, as the local references str2 and str3 in the main() method and the static reference str1 in the Auxiliary class all denote the same interned string, the call to the intern() method on any of these String objects will return the same canonical string from the pool. The method call str4.intern() will find that a string with its contents (“You cannot change me”) already exists in the string pool, and will also return this string from the pool. Thus the object reference comparison str1.intern() == str4.intern() will return true, but str4 == str4.intern() is still false, as the String object denoted by str4 is not interned and therefore does not denote the same String object as the one that is interned.

String intern()

If the string pool already contains a string that is equal to this String object (determined by the equals(Object) method), then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

Example 8.3 String Construction and Internment

Click here to view code image

// File: StringConstruction.java
class Auxiliary {
  static String str1 = “You cannot change me!”;          // Interned
}
//______________________________________________________________________________
public class StringConstruction {
  static String str1 = “You cannot change me!”;          // Interned
  public static void main(String[] args) {
    String emptyStr = new String();                      // “”
    System.out.println(“emptyStr: \”” + emptyStr + “\””);
    String str2 = “You cannot change me!”;               // Interned
    String str3 = “You cannot” + ” change me!”;          // Interned
    String str4 = new String(“You cannot change me!”);   // New String object
    String words = ” change me!”;
    String str5 = “You cannot” + words;                  // New String object
    System.out.println(“str1 == str2:      ” +  (str1 == str2));     // (1) true
    System.out.println(“str1.equals(str2): ” +  str1.equals(str2));  // (2) true
    System.out.println(“str1 == str3:      ” + (str1 == str3));      // (3) true
    System.out.println(“str1.equals(str3): ” + str1.equals(str3));   // (4) true
    System.out.println(“str1 == str4:      ” + (str1 == str4));      // (5) false
    System.out.println(“str1.equals(str4): ” + str1.equals(str4));   // (6) true
    System.out.println(“str1 == str5:      ” + (str1 == str5));      // (7) false
    System.out.println(“str1.equals(str5): ” + str1.equals(str5));   // (8) true
    System.out.println(“str1 == Auxiliary.str1:      ” +
                       (str1 == Auxiliary.str1));        // (9) true
    System.out.println(“str1.equals(Auxiliary.str1): ” +
                        str1.equals(Auxiliary.str1));    // (10) true
    System.out.println(“\”You cannot change me!\”.length(): ” +
                       “You cannot change me!”.length());// (11) 21
    System.out.println(“str1.intern() == str4.intern(): ” +
                       (str1.intern() == str4.intern()));// (12) true
    System.out.println(“str4 == str4.intern(): ” +
                       (str4 == str4.intern()));         // (13) false
  }
}

Output from the program:

Click here to view code image emptyStr: “”
str1 == str2:      true
str1.equals(str2): true
str1 == str3:      true
str1.equals(str3): true
str1 == str4:      false
str1.equals(str4): true
str1 == str5:      false
str1.equals(str5): true
str1 == Auxiliary.str1:      true
str1.equals(Auxiliary.str1): true
“You cannot change me!”.length(): 21
str1.intern() == str4.intern(): true
str4 == str4.intern(): false


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *