Lesson 11 - String Class
INTRODUCTION: |
Strings are needed in many large programming tasks. Much of the information which identifies a person must be stored as a string: name, address, city, social security number, etc. A standard
A. The String Class
|
|||||||||||||||||||||||||||||||||||||
VOCABULARY: |
none |
|
||||||||||||||||||||||||||||||||||||
DISCUSSION: |
A. The String Class 1.Character strings in Java are not represented by a primitive types
as are integers (int) or single characters (char). Strings
are represented as objects of the 2. A "This is a string" The characters that a String object contains can include control characters. The last example contains a tab (\t) and a linefeed (\n) character, specified with escape sequences. 3. A second unique characteristic of the String type is that it supports
the "+" operator to concatenate two String expressions. For
example: The "+" operator can be used to combine a String expression
with any other expression of primitive type. When this occurs, the primitive
expression is converted to a String representation and concatenated
with the string. For example, consider the following instruction sequence: PI = 3.14159; System.out.println("The value of PI is " + PI); Run output: The value of PI is 3.14159 4. String objects are immutable. This means that after construction,
they cannot be altered. 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 5. 6. The rest of the student outline details a partial list of the constructors,
and methods, provided by the Java
B. String Constructors 1. You create a String object by using the keyword new and the
2. Though they are not primitive types, strings are so important and frequently used that Java provides an addition syntax for declaration: String aGreeting = "Hello world"; A C. String Query Methods
1. The method int length(); returns the number of characters in the String object. 2. The method charAt method is a tool for extracting a character from
within a String. The charAt parameter specifies the position of the
desired char (0 for the leftmost character, 1 for the second from
the left, etc.). For example, executing the following two instructions,
prints the char value 'X'. String stringVar = "VWXYZ" System.out.println(stringVar.charAt(2); 3. The method int 4. The method int D. String Translate Methods
1. 2. 3. 4. 5. E. Comparing Strings 1. Because String variables hold object references, you cannot make a simple comparison to determine whether two String objects are equivalent. If you declare two Strings as String aGreeting = "Hello"; String anotherGreeting = "Hello"; Java will evaluate a comparison such as If (aGreeting == anotherGreeting) ... as Fortunately, the String class provides you with a number of useful methods.
2.The String aName = "Mat"; String anotherName = "Mat"; if (aName.equals(anotherName)) System.out.println("Name's the same"); if (anotherName.equals(aName)) System.out.println("Name's the same"); if (aName.equals("Mat")) System.out.println("Name's the same"); Each String shown above- 3. The equalsIgnoreCase() method is very similar to the equals() method. As its name implies, it ignores case when determining if two Strings are equivalent. This method is very useful when users type responses to prompts in your program. The equalsIgnoreCase() method allow you to test entered data without regard to capitalization. 4. The compareTo() method compares the calling string object and the
string argument to see which comes first in the lexicographic ordering.
Lexicographic ordering is the same as alphabetical ordering when both
strings are either all uppercase or all lowercase. If the calling string
is first, it returns a negative value. If the two strings are equal,
it returns zero. If the arguments is first, it returns a positive number.
F. Strings and Characters. 1. It is natural to think of a char as a
The last restriction is particularly troublesome, because there are
many times in programs when char data and 2.Extracting a char from within a String can be accomplished using the charAt method as previously described. 3. Conversion from char to String can be accomplished by using the "+" (concatenation) operator described previously. Concatenating any char with an empty string (String of length zero) results in a string that consists of that char. The java notation for an empty string is two consecutive double quotation marks. For example, the following expression
evaluates to a String of length 1, consisting of the letter "X" // charVar is a variable of type char // stringVar is a variable of type String stringVar = "" + charVar; The execution of this instruction assigns to
|
|||||||||||||||||||||||||||||||||||||
SUMMARY/ REVIEW: |
The use of pre-existing code (classes) has helped reduce the time and cost of developing new programs. In this lesson you will use the String class without knowing its inner details. This is an excellent example of data type abstraction. |