Wednesday, December 7, 2011

Java's character and assorted string classes support text-processing - 7


What creates that String object and when? Neither the Java Language Specification nor the Java Virtual Machine Specification offer answers that I can find. Instead, I speculate the following: When a classloader—a concept I'll discuss in a future article—loads a class file, it scans its constant pool's memory copy. For each string literal in that pool, the classloader creates a String, populates that object with the string literal's characters, and modifies the string literal's entry in the constant pool's memory copy so ldc pushes the String's reference onto the calling thread's stack.
Because the compiler and classloader treat string literals as String objects, "abc".length() and synchronized ("sync object") are legal. "abc".length() returns the length of the String containing abc; and synchronized ("sync object") grabs the lock associated with the String containing sync object. Java regards these and other string literals as String objects to serve as a convenience for developers. As with the simplified assignment shortcut, substituting string literals for String object reference variables reduces the amount of code you must write.
Java also offers a variety of String constructors for creating String objects. I detail three below:
  1. public String(char [] value) creates a new String object that contains a copy of all characters found in the value array parameter. If value is null, this constructor throws a NullPointerException object.
  2. public String(char [] value, int offset, int count) creates a new String that contains a portion of those characters found in value. Copying begins at the offset array index and continues for count characters. If value is null, this constructor throws a NullPointerException object. If either offset or count contain values that lead to invalid array indexes, this constructor throws an IndexOutOfBoundsException object.
public String(String original) creates a new String that contains the same characters as the original-referenced String.

No comments: