CSCI A201/A597

Lecture Notes 14

Spring 2000


Review of material for the midterm exam. We went over some of the exercises in the first 6 batches of QuizSite items (for week 5) including String type of objects.

Because we had a practical exam that evening we started by looking at this pattern:

and identified it as
((i == (size - 1) / 2) && (j <= (size - 1) * 3 / 4) ||
 (j == (size - 1) / 2) && (i >= (size - 1) * 1 / 4) ||
 (i + j == (size - 1) / 2))
Then we went into the midterm review.

First we start by reviewing Strings. You've seen String constants before, they are sequences of characters surrounded by double quotes ("), such as:

"abc"
You can assign such values to variables of adequate type:
String a = "abc";
You can perform operations on them such as:
String b;
b = a + "mnp"; 
A new string (containing the concatenation of "abc" and "mnp") will be created and the reference to it will be stored in the variable b. In this process the String pointed to by a will remain unchanged.

Strings are not primitive types. They are a bit more complex: you can ask Strings questions and get back answers. All you have to do when asking the questions is to know the syntax and follow it carefully.

For example, Strings can tell you their length in characters:

System.out.println(a.length());
will print 3.

Likewise

System.out.println("abc".length());
will also print 3.

Strings can retrieve for you their ith character if you tell them the character's index, i and, of course, if the string is long enough to have a character with that index.

So,

"xyz".charAt(0)
will evaluate to the character 'x'.

There are at least two ways in which you can extract substrings out of a given String:

  1. by extracting all the characters that follow the character located at a certain index i, or

  2. by extracting all successive characters starting at the character with index i all the way to the one with index j (and without the character located at index j)
String's come with this built in ability, and to obtain a substring in the first manner you say:
a.substring(<int>)
while for the second approach the invocation goes like this:
a.substring(<int>, <int>)
Thus if a points to "abcdefghijk"
a.substring(5)
evaluates to
"fghijk"
and
a.substring(3, 6)
evaluates to
"def"
So to obtain
"bean"
from
String a = "boolean";
we could evaluate the expression
a.substring(0, 1) + a.substring(4)
Once again, Strings, as explained above, are given. The fact that String objects have the ability to understand the following commands, and take action and provide answers to them, is due to the creators of the java.lang package. In other words String object are predefined in Java and you need to know them to use them effectively.

String also know if they have similar spelling with another String (and they will return true or false when asked about it) as long as the other String is made explicit.

That means that

x.equals(x)
as well as
x.equals("abc"); 
will both return true, and
x.equals("aBc"); 
or
x.equals("xyz"); 
will return false.

"abc".equals("a" + "b" + "c");
will all return true.

Here now is a picture that shows the situation after:

String x, y;
x = "abc";
y = "ab" + "c";
The picture looks like this:

There are two different String objects that will look the same but be stored at different places in the memory. They will be pointed to by x and y, both variables of type String.

The test

x == y
will yield false (because the objects are distinct), while
x.equals(y)
will yield true (since the strings are composed of the same letters, in the exact same case, and in the same order).

Of course, if we change x to point to y

x = y;
then both
x == y
and
x.equals(y)
will yield true and the first circle is no longer pointed by anyone, and so it gets discarded.

One last thing about Strings was that they also have the ability of creating uppercased or lowercased versions of themselves. For example:

String z;
z = x.toUpperCase(); 
will create a new String, with the value of "ABC", and store the address (pointer to it) in a memory location by the name of z).

One other thing that we discussed was an example like the following.

Consider this code:

if (<cond1>) {
  if (<cond2>) 
    <stat1>
} else 
  <stat2>
with and without parentheses.

The diagrams are represented below, next to each other:

With {'s:
<stat2> gets executed when <cond1> is false, regardless of the value of <cond2>
Without {'s:
<stat2> will get executed when <cond1 is true and <cond2> is false only


Last modified on March 2, 2000 by Adrian German