Strings
Home - About Us
Strings More Strings

String Objects and References

Strings are are built-in types, such as int and double.  Strings are objects.  Therefore, the common practice is to declare a String reference:

	String str;

and then instantiate a String object with the following statement:

	str = "tree";

In Java the expression "tree" is roughly equivalent to the expression new String("tree"). Both expressions create a new String object that contins the word "tree", and return a reference to that newly created object.

In addition to storing the characters of a string, Java String objects also contain dozens of methods.  Therefore, a more accurate visualization of the above two lines of Java code would be this:

 

Before delving into the details of some of the String methods, it is useful to highlight the following:

  1. String objects are immutable -- once a String object has been created its contents cannot change.

  2. Many String methods create a new String object (and return a reference to that newly created object).
  3. A declaration statement such as
    		String s1;
    does not create a String object -- it only creates a reference variable.


  4. The "==" operator only returns true if the same object is referenced on both sides of the == operator. You must use the String equals function to test if two String objects store the same word.

This section will only introduce a handful of String methods.  A full list of the String methods can be found at Sun Microsystem's documentation of the Java Application Program Interface (API). The Java API lists all of the Java classes supplied by Sun Microsystems -- which is a lot of classes.  Click on the name of a class in the bottom left hand panel and the full documentation for that class is displayed (for example you could click on the String class to see the documentation on all of the String methods available for us to use).

The String methods introduced here are:

  • toUpperCase()
  • toLowerCase()
  • substring(int from, int to)
  • replace(char from, char to)
  • trim()
  • indexOf(char ch)
  • length()
  • equals(String s)

Additionally, using the + and == operators with Strings will be discussed.

Example

The following code creates four String references and three String objects:

	String s1;
	String s2 = "the";
	String s3 = "";
	String s4 = new String("over");

With the following visualization:

The reference variable s1 does not refer to any object -- no String object was instantiated. The reference s2 refers to a String object containing the three character word "the". The reference variable s3 refer to a String object, with no characters in it. Even though s3 has no characters, it is a full String object containing all of the normal String methods.  The reference s4 references a String object containing the word "over".

Example

In this example the variable str is assigned a reference to the String "rock".  The str variable is then changed so that it refers to the String "sky".  The change from "rock" to "sky" changes the str reference variable, not either of the objects "rock" or "sky":

	String str;
	str = "rock";
	str = "sky";

Visualization:  click here for a Flash animation of the above code .  In this animation the red "X" drawn on the "rock" object indicates that the Java garbage collector will eventual collect the unreferenced "rock" object (in Java an unreferenced object is useless and eventually disposed of by a mechanism known as "garbage collection").

String Methods That Yield New Strings

The String methods toUpperCase, toLowerCase, substring, replace and trim yield new String objects.  For example, in the code below the first instruction creates a String object containing the word "tea" and a reference to that object:

	String s1 = "tea";
	String s2 = s1.toUpperCase();

In the second line of code, the toUpperCase method inside the "tea" String object creates a new String object -- this new object is just like the "tea" object except the characters are uppercase.  The s2 variable saves the reference to this newly created object:

 

Similarly, the replace method will replace all occurences of one character in a string with another character.  For example, all occurences of 'e' are set to 'A' in the newly created String object:

	String word = "forever";
	String changed = word.replace('e', 'A');
	System.out.println(word);
	System.out.println(changed);

The output for the above code is:

	forever
	forAvAr

The substring method pulls out a portion of an existing String.   For example, the following code pulls the word "ran" out of the string "orange":

	String s = "orange";
	String out = s.substring(1, 4);

The bounds of the substring are specified by indices.  Each letter in a String has an index, as shown below for the String orange.  The substring method specifies the index of the first letter of the substring, and the index of the letter just past the end of the desired substring.  In our example substring(1,4) indicated that we wanted the letters from index 1 up to, but not including, index 4.  In the figure below, the indices of the characters are indicated in red numbers below the characters:

The trim method removes any whitespace at the start or end of a string (whitespace refers to blanks, tabs and newline characters).  Therefore, if we had the string str:

	String str = "    and    ";

Then the following call to the trim method

	String s2 = str.trim();

would yield the new string "and" (with the blanks removed from the start and end of the string).

Programmers commonly re-use one reference variable to keep track of a succession of String objects.  For example, if str is referencing the String "freeze":

	String str = "freeze";

and we want to pull the first four letters out of that word, a common technique is to have str drop its reference to "freeze" and have it reference the newly create string "free":

	str = str.substring(0,4);

String Methods That Return An Integer Value

The indexOf method returns the index of the first occurence of a letter in a word.  For example, after the following instruction is performed the value 2 is assigned to the variable i:

	String s = "orange";
	int i = s.indexOf('a');

indexOf returns a -1 if the character does not occur in the word, such as in the following example:

	String s = "orange";
	int i = s.indexOf('x');

The length method returns the size of the word (the total number of characters in the word).  The word "orange" has six characters, so the variable length in the following example is assigned the value six:

	String s = "orange";
	int length = s.length();

The equals Method

The == operator compares the values of built-in types, such as for int and double values:

	int x = 5;
	int y = 5;
	if (x == y)
		System.out.println("Same");
	else
		System.out.println("Different");

The expression x == y will yield true in the above example.  However, when comparing references the == operator returns true only if the references are to the same object.  Comparing the references to two different String objects with the == operator will yield false, even if the String objects contain the same word.  In the example below the expression word1 == word2 will yield false:

		String word1 = new String("the");
		String word2 = new String("the");
		
		if (word1 == word2)
			System.out.println("Same.");
		else
			System.out.println("Different.");

The above code prints "Different" to the console.

To see if two different String objects have the same contents you must use the equals method:

		String word1 = new String("the");
		String word2 = new String("the");
		
		if (word1.equals(word2))
			System.out.println("Same.");
		else
			System.out.println("Different.");

The above code prints the word "Same" to the console.

Note that in the two previous examples I used the expression new String("the") to create the String objects instead of the simple expression "the".  Even though the expressions "the" and new String("the") have similar effects -- they both create a String object and return a reference to that object -- the expressions are not equivalent.  The expression new String("the") always creates a brand new String object. The expression "the" is a request to Java to create a new String object containing the word "the", unless a "the" String object was previously declared with this same expression (then just reuse that earlier object). Therefore, these two Java lines create two references word1 and word2 and two distinct String objects:

		String word1 = new String("the");
		String word2 = new String("the");

whereas these two lines only create one String object which is shared by the two references word1 and word2:

		String word1 = "the";
		String word2 = "the";

The + Operator

The + operator can be used to concatenate two String objects.  The example below creates three String objects: "blue", "sky" and "bluesky":

	String s1 = "blue";
	String s2 = "sky";
	String s3 = s1 + s2;

Method Calls Within Method Calls

The indexOf and length methods are commonly used within a call to the substring method. For example, if we want to pull out all of the characters before "ing" in "evaluating" we need to find find out the index of the character 'i'.

	String s = "evaluating";

Instead of counting out the indices by hand, we'll let the indexOf method give the find the index, and pass that index directly to the substring method:

	String s2 = s.substring(0, s.indexOf('i'));

Or, if wanted to pull out just the "ing" from evaluating, we can tell let indexOf determine the index of the letter 'i', and have length determine the index past the end of the word:

	String s2 = s.substring(s.indexOf('i'),  s.length());

In this case, s.indexOf('i') will return a seven, and s.length() will return a ten -- those will get plugged into the call to substring to give s.substring(7, 10), which will pull the word "ing" out of "evaluating".

Home - About Us
Copyright © 2006 by Kiowok, Ann Arbor, Michigan, USA