When we introduced strings earlier in this chapter, we pointed out a strange feature of that data type: to operate on strings, we use object notation. For example, a typical operation involving strings might be the following:
s = "These are the times that try people's souls."; last_word = s.substring(s.lastIndexOf(" ")+1, s.length);
In Chapter 6, Functions, we'll see something similar: functions also have properties that we can access using object notation. What's going on? Are strings and functions objects, or are they distinct data types? In Navigator 3.0, the typeof operator assures us that strings have a data type "string" and that functions are of type "function" and that neither is of type "object". Why then, do they use object notation?
When we use a string in an object context, note that the String object that is created is a transient one--it is used to allow us to access a property or method, and then it is no longer needed and is reclaimed by the system. Suppose s is a string, and we determine the length of the string with a line like this:
len = s.length;
If for some reason we want to use a String object explicitly in our program, we will have to create a non-transient one that will not be automatically discarded by the system. We create String objects just as we create other objects, with the new operator. (The new operator will be introduced in Chapter 4, Expressions and Operators, and we'll learn more about object creation in Chapter 7, Objects.) For example:
s = "hello world"; // a primitive string value S = new String("Hello World"); // a String object
msg = S + '!';