Strings

advertisement
Strings and Object References
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."; // rather than: String zeta = new String(“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. Other classes do not have short-cuts like this.
Objects of most classes are constructed by using the new operator.
String
References as Parameters
Some methods require a parameter that is a reference to a
String object. For example,
String stringA = "Random Jottings";
String stringB = "Lyrical Ballads";
if ( stringA.equals( stringB ) )
System.out.println("They are equal.");
else
System.out.println("They are different.");
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.
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;
Null Assigned to any Reference Variable
It would be awful if each class had its own special value to show that no object of that class was
present. We need a universal value that means "nothing here". The value null works for this and
can be assigned to any reference variable.
In most programs, objects come and objects go, depending on the data and on what is being
computed. (Think of a computer game, where monsters show up, and monsters get killed). A
reference variable sometimes does and sometimes does not refer to an object, and can refer to
different objects at different times. You need a way to say that a variable does not now refer to an
object. You do this by assigning null to the variable.
The Empty String
A String object that contains no characters is still an object. Such an object is called an empty
string. It is similar to having a blank sheet of paper, versus having no paper at all. Overlooking this
distinction is one of the classic confusions of computer programming.
Here is a short version of the documentation for String.
// Constructors
public String();
public String(String value);
// Methods
public char charAt(int index);
public String concat(String str);
public boolean endsWith(String suffix);
public boolean equals(Object anObject);
public boolean equalsIgnoreCase(String anotherString);
public int indexOf(int ch);
public int indexOf(String str);
public int length();
public boolean startsWith(String prefix);
public String substring(int beginIndex );
public String substring(int beginIndex, int endIndex);
public String toLowerCase();
public String toUpperCase();
public String trim();
The documentation first lists constructors. Then it describes the methods. For example,
public String concat(String str); // note this more commonly done like str1 + str2 as opposed to str1.concat(str2);
--+--- --+--- --+-- ----+---|
|
|
|
|
|
|
|
|
|
|
+---- says that the method requires a
|
|
|
|
|
|
|
|
+----- the name of the method
|
|
|
+----- the method returns a reference
|
String reference parameter
to a new String object
|
+----- anywhere you have a String object,
you can use this method
Strings are Forever
Java was designed after programmers had about 15 years of experience with object oriented
programming. One of the lessons learned in those years is that it is safer to construct a new object
rather than to modify an old one. (This is because many places in the program might refer to the old
object, and it is hard to be sure that they will all work correctly when the object is changed.)
Objects of some Java classes cannot be altered after construction. The class String is one of
these.
String objects are immutable. This means that after construction, a
String object cannot be altered.
Sometimes immutable objects are called write-once objects. Once a String object has been
constructed, the characters it contains will always be the same. None of its methods will change
those characters, and there is no other way to change them. The characters can be used for various
purposes (such as in constructing new String objects), and can be inspected. But they can
never be altered.
Confusion Alert!! This is a place where it is important to distinguish between reference
variables and their objects. A reference variable referring to a String can be altered
(it can be made to point to a different String object.) The String object it refers
to cannot be altered.
"Changing" a String
A common mistake is to think "change a string", but to then attempt to change an immutable object.
When you think "change a string" you need to do two things:
1. Compute a new String object.
2. Assign the reference to the new String to a reference variable.
String Indexing
The beginning character of a string corresponds to index 0 and the last character corresponds to the
index (length of string)-1.
// str.length()-1
The length of a string is the number of characters it contains, including spaces, punctuation, and
control characters
Download