| CSCI A201/A597Lecture Notes 14 Spring 2000 |
String type of objects.
Because we had a practical exam that evening we started by looking at this pattern:
and identified it as![]()
Then we went into the midterm review.((i == (size - 1) / 2) && (j <= (size - 1) * 3 / 4) || (j == (size - 1) / 2) && (i >= (size - 1) * 1 / 4) || (i + j == (size - 1) / 2))
First we start by reviewing Strings. You've seen
String constants before, they are sequences of
characters surrounded by double quotes ("), such as:
You can assign such values to variables of adequate type:"abc"
You can perform operations on them such as:String a = "abc";
A new string (containing the concatenation ofString b; b = a + "mnp";
"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:
will printSystem.out.println(a.length());
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,
will evaluate to the"xyz".charAt(0)
character 'x'.
There are at least two ways in which you can extract substrings
out of a given String:
i, or
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:
while for the second approach the invocation goes like this:a.substring(<int>)
Thus ifa.substring(<int>, <int>)
a points to "abcdefghijk"
evaluates toa.substring(5)
and"fghijk"
evaluates toa.substring(3, 6)
So to obtain"def"
from"bean"
we could evaluate the expressionString a = "boolean";
Once again,a.substring(0, 1) + a.substring(4)
Strings, as explained above, are
given. The fact that String objects have
the ability to understand the following commands,
length()
charAt(<int>)
substring(<int>)
substring(<int>, <int>)
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
as well asx.equals(x)
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:
The picture looks like this:String x, y; x = "abc"; y = "ab" + "c";
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
will yieldx == y
false (because the objects are distinct), while
will yieldx.equals(y)
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
then bothx = y;
andx == y
will yieldx.equals(y)
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:
will create a newString z; z = x.toUpperCase();
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:
|
Without {'s:
|