“Chapter 29: String and Object References” Bradley Kjell (Revised 06/15 ...

"Chapter 29: String and Object References" Bradley Kjell (Revised 06/15/2008)

In previous chapters, methods were called with parameters that were primitive data types. This chapter discusses how to use object references as parameters. The class String is used in many examples.

Chapter Topics: ? String literals ? The null value ? More about garbage ? The String class ? String concatenation ? Strings are immutable ? Some String methods o concat() o length() o trim() o substring() o toLowerCase() o startsWith() o charAt() ? Cascading methods

Students taking the computer science Advanced Placement examination are expected to be familiar with the String class.

QUESTION 1: (Review:) What TWO things does the following statement do? String zeta = new String( "The last rose of summer." );

Source URL: Saylor URL: ? Bradley Kjell Used by permission.

Page 1 of 38

Answer: A new String object is created, containing the characters between quote

marks. A reference to the object is saved in the variable zeta.

Easy way to Construct Strings

String objects are very useful and are frequently used. To make life easier for programmers, Java has a short-cut way of creating a String object: String zeta = "The last rose of summer." ; This creates a String object containing the characters between quote marks. A String created in this short-cut way is called a String literal. Remember that if several statements in a program ask for literals containing the same characters, only one object will actually be constructed (see chapter 26). Other classes do not have short-cuts like this. Objects of most classes are constructed by using the new operator.

QUESTION 2: Can a String reference be a parameter for a method?

Source URL: Saylor URL: ? Bradley Kjell Used by permission.

Page 2 of 38

Answer: Yes, a String reference is often a parameter. String

References as Parameters

Some methods require a parameter that is a reference to a String object. For example,

The picture that shows how the method call works. (Both objects have many methods, but only the equals() method of one object is pictured.) The String object referred to by stringA has an equals() method. That method is called with a parameter, a reference to another String object, stringB. The method checks if both String objects contain identical sequences of characters, and if so, evaluates to true.

Careful: The previous paragraph is correctly stated, but awkward. People often say "String" when they really mean "reference to a String". This is fine, but remember that a reference variable like stringA does not contain an object, but only a reference to an object. This may seem picky, but there is nothing quite as picky as a computer. Students who are not careful about this often run into problems.

Source URL: Saylor URL: ? Bradley Kjell Used by permission.

Page 3 of 38

QUESTION 3: (Review:) Examine the following snippet of code. Answer the questions using careful words (like the above on the right). String a; Point b;

What is the data type of the variable a? What is the data type of the variable b? How many objects are there (so far)?

Source URL: Saylor URL: ? Bradley Kjell Used by permission.

Page 4 of 38

Answer: ? What is the data type of the variable a? o a is a reference to a String object. ? What is the data type of the variable b? o b is a reference to a Point object. ? How many objects are there (so far)? o So far, there are no objects, just variables that can reference objects, once there are some.

The null Value

A reference variable holds information about the location of an object. It does not hold the object itself. This code

String a; Point b;

declares two reference variables but does not construct any objects. The following constructs objects and puts references in the variables:

a = "Elaine the fair" ; b = new Point( 23, 491 );

null is a special value that means "no object." Your program should set a reference variable to null when it is not referring to any object. Programs often set variables to null when they are declared:

String a = null; Point b = null;

QUESTION 4: (Thought Question:) Do you think that null can be assigned to reference variables of any type?

Hint: How many types of nothing are there?

Source URL: Saylor URL: ? Bradley Kjell Used by permission.

Page 5 of 38

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download